Lah numbers: Difference between revisions

Add Swift
(Undo vandalism to task description)
(Add Swift)
Line 779:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
</pre>
 
 
=={{header|Swift}}==
 
{{libheader|AttaSwift BigInt}}
{{trans|Kotlin}}
 
<lang swift>import BigInt
import Foundation
 
@inlinable
public func factorial<T: BinaryInteger>(_ n: T) -> T {
guard n != 0 else {
return 1
}
 
return stride(from: n, to: 0, by: -1).reduce(1, *)
}
 
@inlinable
public func lah<T: BinaryInteger>(n: T, k: T) -> T {
if k == 1 {
return factorial(n)
} else if k == n {
return 1
} else if k > n {
return 0
} else if k < 1 || n < 1 {
return 0
} else {
let a = (factorial(n) * factorial(n - 1))
let b = (factorial(k) * factorial(k - 1))
let c = factorial(n - k)
 
return a / b / c
}
}
 
print("Unsigned Lah numbers: L(n, k):")
print("n\\k", terminator: "")
 
for i in 0...12 {
print(String(format: "%10d", i), terminator: " ")
}
 
print()
 
for row in 0...12 {
print(String(format: "%-2d", row), terminator: "")
 
for i in 0...row {
lah(n: BigInt(row), k: BigInt(i)).description.withCString {str in
print(String(format: "%11s", str), terminator: "")
}
}
 
print()
}
 
let maxLah = (0...100).map({ lah(n: BigInt(100), k: BigInt($0)) }).max()!
 
print("Maximum value from the L(100, *) row: \(maxLah)")</lang>
 
{{out}}
 
 
<pre>Unsigned Lah numbers: L(n, k):
n\k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
Maximum value from the L(100, *) row: 44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|zkl}}==