Bell numbers: Difference between revisions
Content added Content deleted
(Add Swift) |
|||
Line 974: | Line 974: | ||
(same output as above) |
(same output as above) |
||
=={{header|Swift}}== |
|||
{{trans|Kotlin}} |
|||
<lang swift>public struct BellTriangle<T: BinaryInteger> { |
|||
@usableFromInline |
|||
var arr: [T] |
|||
@inlinable |
|||
public internal(set) subscript(row row: Int, col col: Int) -> T { |
|||
get { arr[row * (row - 1) / 2 + col] } |
|||
set { arr[row * (row - 1) / 2 + col] = newValue } |
|||
} |
|||
@inlinable |
|||
public init(n: Int) { |
|||
arr = Array(repeating: 0, count: n * (n + 1) / 2) |
|||
self[row: 1, col: 0] = 1 |
|||
for i in 2...n { |
|||
self[row: i, col: 0] = self[row: i - 1, col: i - 2] |
|||
for j in 1..<i { |
|||
self[row: i, col: j] = self[row: i, col: j - 1] + self[row: i - 1, col: j - 1] |
|||
} |
|||
} |
|||
} |
|||
} |
|||
let tri = BellTriangle<Int>(n: 15) |
|||
print("First 15 Bell numbers:") |
|||
for i in 1...15 { |
|||
print("\(i): \(tri[row: i, col: 0])") |
|||
} |
|||
for i in 1...10 { |
|||
print(tri[row: i, col: 0], terminator: "") |
|||
for j in 1..<i { |
|||
print(", \(tri[row: i, col: j])", terminator: "") |
|||
} |
|||
print() |
|||
}</lang> |
|||
{{out}} |
|||
<pre>First 15 Bell numbers: |
|||
1: 1 |
|||
2: 1 |
|||
3: 2 |
|||
4: 5 |
|||
5: 15 |
|||
6: 52 |
|||
7: 203 |
|||
8: 877 |
|||
9: 4140 |
|||
10: 21147 |
|||
11: 115975 |
|||
12: 678570 |
|||
13: 4213597 |
|||
14: 27644437 |
|||
15: 190899322 |
|||
1 |
|||
1, 2 |
|||
2, 3, 5 |
|||
5, 7, 10, 15 |
|||
15, 20, 27, 37, 52 |
|||
52, 67, 87, 114, 151, 203 |
|||
203, 255, 322, 409, 523, 674, 877 |
|||
877, 1080, 1335, 1657, 2066, 2589, 3263, 4140 |
|||
4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147 |
|||
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |