Skip to content

Commit 002f9c6

Browse files
committed
Updated ReadMe
Replaced Array2D with Swift inbuilt 2D array
1 parent 4c7920d commit 002f9c6

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Combinatorics/README.markdown

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -366,26 +366,26 @@ The following code calculates Pascal's triangle in order to find the `C(n, k)` y
366366

367367
```swift
368368
func binomialCoefficient(n: Int, _ k: Int) -> Int {
369-
var bc = Array2D(columns: n + 1, rows: n + 1, initialValue: 0)
370-
371-
for i in 0...n {
372-
bc[i, 0] = 1
373-
bc[i, i] = 1
374-
}
375-
376-
if n > 0 {
377-
for i in 1...n {
378-
for j in 1..<i {
379-
bc[i, j] = bc[i - 1, j - 1] + bc[i - 1, j]
380-
}
369+
var bc = Array(count: n + 1, repeatedValue: Array(count: n + 1, repeatedValue: 0))
370+
371+
for i in 0...n {
372+
bc[i][0] = 1
373+
bc[i][i] = 1
381374
}
382-
}
383-
384-
return bc[n, k]
375+
376+
if n > 0 {
377+
for i in 1...n {
378+
for j in 1..<i {
379+
bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]
380+
}
381+
}
382+
}
383+
384+
return bc[n][k]
385385
}
386386
```
387387

388-
This uses [Array2D](../Array2D/) as helper code because Swift doesn't have a built-in two-dimensional array. The algorithm itself is quite simple: the first loop fills in the 1s at the outer edges of the triangle. The other loops calculate each number in the triangle by adding up the two numbers from the previous row.
388+
The algorithm itself is quite simple: the first loop fills in the 1s at the outer edges of the triangle. The other loops calculate each number in the triangle by adding up the two numbers from the previous row.
389389

390390
Now you can calculate `C(66, 33)` without any problems:
391391

0 commit comments

Comments
 (0)