2
2
3
3
/* Calculates n! */
4
4
func factorial( n: Int ) -> Int {
5
- var n = n
6
- var result = 1
7
- while n > 1 {
8
- result *= n
9
- n -= 1
10
- }
11
- return result
5
+ var n = n
6
+ var result = 1
7
+ while n > 1 {
8
+ result *= n
9
+ n -= 1
10
+ }
11
+ return result
12
12
}
13
13
14
14
factorial ( 5 )
@@ -17,17 +17,17 @@ factorial(20)
17
17
18
18
19
19
/*
20
- Calculates P(n, k), the number of permutations of n distinct symbols
21
- in groups of size k.
22
- */
20
+ Calculates P(n, k), the number of permutations of n distinct symbols
21
+ in groups of size k.
22
+ */
23
23
func permutations( n: Int , _ k: Int ) -> Int {
24
- var n = n
25
- var answer = n
26
- for _ in 1 ..< k {
27
- n -= 1
28
- answer *= n
29
- }
30
- return answer
24
+ var n = n
25
+ var answer = n
26
+ for _ in 1 ..< k {
27
+ n -= 1
28
+ answer *= n
29
+ }
30
+ return answer
31
31
}
32
32
33
33
permutations ( 5 , 3 )
@@ -37,22 +37,22 @@ permutations(9, 4)
37
37
38
38
39
39
/*
40
- Prints out all the permutations of the given array.
41
- Original algorithm by Niklaus Wirth.
42
- See also Dr.Dobb's Magazine June 1993, Algorithm Alley
43
- */
40
+ Prints out all the permutations of the given array.
41
+ Original algorithm by Niklaus Wirth.
42
+ See also Dr.Dobb's Magazine June 1993, Algorithm Alley
43
+ */
44
44
func permuteWirth< T> ( a: [ T ] , _ n: Int ) {
45
- if n == 0 {
46
- print ( a) // display the current permutation
47
- } else {
48
- var a = a
49
- permuteWirth ( a, n - 1 )
50
- for i in 0 ..< n {
51
- swap ( & a[ i] , & a[ n] )
52
- permuteWirth ( a, n - 1 )
53
- swap ( & a[ i] , & a[ n] )
45
+ if n == 0 {
46
+ print ( a) // display the current permutation
47
+ } else {
48
+ var a = a
49
+ permuteWirth ( a, n - 1 )
50
+ for i in 0 ..< n {
51
+ swap ( & a[ i] , & a[ n] )
52
+ permuteWirth ( a, n - 1 )
53
+ swap ( & a[ i] , & a[ n] )
54
+ }
54
55
}
55
- }
56
56
}
57
57
58
58
let letters = [ " a " , " b " , " c " , " d " , " e " ]
@@ -66,30 +66,30 @@ permuteWirth(xyz, 2)
66
66
67
67
68
68
/*
69
- Prints out all the permutations of an n-element collection.
70
-
71
- The initial array must be initialized with all zeros. The algorithm
72
- uses 0 as a flag that indicates more work to be done on each level
73
- of the recursion.
74
-
75
- Original algorithm by Robert Sedgewick.
76
- See also Dr.Dobb's Magazine June 1993, Algorithm Alley
77
- */
69
+ Prints out all the permutations of an n-element collection.
70
+
71
+ The initial array must be initialized with all zeros. The algorithm
72
+ uses 0 as a flag that indicates more work to be done on each level
73
+ of the recursion.
74
+
75
+ Original algorithm by Robert Sedgewick.
76
+ See also Dr.Dobb's Magazine June 1993, Algorithm Alley
77
+ */
78
78
func permuteSedgewick( a: [ Int ] , _ n: Int , inout _ pos: Int ) {
79
- var a = a
80
- pos += 1
81
- a [ n] = pos
82
- if pos == a. count - 1 {
83
- print ( a) // display the current permutation
84
- } else {
85
- for i in 0 ..< a. count {
86
- if a [ i] == 0 {
87
- permuteSedgewick ( a, i, & pos)
88
- }
79
+ var a = a
80
+ pos += 1
81
+ a [ n] = pos
82
+ if pos == a. count - 1 {
83
+ print ( a) // display the current permutation
84
+ } else {
85
+ for i in 0 ..< a. count {
86
+ if a [ i] == 0 {
87
+ permuteSedgewick ( a, i, & pos)
88
+ }
89
+ }
89
90
}
90
- }
91
- pos -= 1
92
- a [ n] = 0
91
+ pos -= 1
92
+ a [ n] = 0
93
93
}
94
94
95
95
print ( " \n Sedgewick permutations: " )
@@ -100,35 +100,35 @@ permuteSedgewick(numbers, 0, &pos)
100
100
101
101
102
102
/*
103
- Calculates C(n, k), or "n-choose-k", i.e. how many different selections
104
- of size k out of a total number of distinct elements (n) you can make.
105
- */
103
+ Calculates C(n, k), or "n-choose-k", i.e. how many different selections
104
+ of size k out of a total number of distinct elements (n) you can make.
105
+ */
106
106
func combinations( n: Int , _ k: Int ) -> Int {
107
- return permutations ( n, k) / factorial( k)
107
+ return permutations ( n, k) / factorial( k)
108
108
}
109
109
110
110
combinations ( 3 , 2 )
111
111
combinations ( 28 , 5 )
112
112
113
113
print ( " \n Combinations: " )
114
114
for i in 1 ... 20 {
115
- print ( " \( 20 ) -choose- \( i) = \( combinations ( 20 , i) ) " )
115
+ print ( " \( 20 ) -choose- \( i) = \( combinations ( 20 , i) ) " )
116
116
}
117
117
118
118
119
119
120
120
/*
121
- Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
122
- k things out of n possibilities.
123
- */
121
+ Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
122
+ k things out of n possibilities.
123
+ */
124
124
func quickBinomialCoefficient( n: Int , _ k: Int ) -> Int {
125
- var result = 1
125
+ var result = 1
126
126
127
- for i in 0 ..< k {
128
- result *= ( n - i)
129
- result /= ( i + 1 )
130
- }
131
- return result
127
+ for i in 0 ..< k {
128
+ result *= ( n - i)
129
+ result /= ( i + 1 )
130
+ }
131
+ return result
132
132
}
133
133
134
134
quickBinomialCoefficient ( 8 , 2 )
@@ -138,47 +138,48 @@ quickBinomialCoefficient(30, 15)
138
138
139
139
/* Supporting code because Swift doesn't have a built-in 2D array. */
140
140
struct Array2D < T> {
141
- let columns : Int
142
- let rows : Int
143
- private var array : [ T ]
144
-
145
- init ( columns: Int , rows: Int , initialValue: T ) {
146
- self . columns = columns
147
- self . rows = rows
148
- array = . init( count: rows*columns, repeatedValue: initialValue)
149
- }
150
-
151
- subscript( column: Int , row: Int ) -> T {
152
- get { return array [ row*columns + column] }
153
- set { array [ row*columns + column] = newValue }
154
- }
141
+ let columns : Int
142
+ let rows : Int
143
+ private var array : [ T ]
144
+
145
+ init ( columns: Int , rows: Int , initialValue: T ) {
146
+ self . columns = columns
147
+ self . rows = rows
148
+ array = . init( count: rows*columns, repeatedValue: initialValue)
149
+ }
150
+
151
+ subscript( column: Int , row: Int ) -> T {
152
+ get { return array [ row*columns + column] }
153
+ set { array [ row*columns + column] = newValue }
154
+ }
155
155
}
156
156
157
157
/*
158
- Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
159
- k things out of n possibilities.
158
+ Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
159
+ k things out of n possibilities.
160
+
161
+ Thanks to the dynamic programming, this algorithm from Skiena allows for
162
+ the calculation of much larger numbers, at the cost of temporary storage
163
+ space for the cached values.
164
+ */
160
165
161
- Thanks to the dynamic programming, this algorithm from Skiena allows for
162
- the calculation of much larger numbers, at the cost of temporary storage
163
- space for the cached values.
164
- */
165
166
func binomialCoefficient( n: Int , _ k: Int ) -> Int {
166
- var bc = Array2D ( columns: n + 1 , rows: n + 1 , initialValue: 0 )
167
-
168
- for i in 0 ... n {
169
- bc [ i, 0 ] = 1
170
- bc [ i, i] = 1
171
- }
172
-
173
- if n > 0 {
174
- for i in 1 ... n {
175
- for j in 1 ..< i {
176
- bc [ i, j] = bc [ i - 1 , j - 1 ] + bc[ i - 1 , j]
177
- }
167
+ var bc = Array ( count: n + 1 , repeatedValue: Array ( count: n + 1 , repeatedValue: 0 ) )
168
+
169
+ for i in 0 ... n {
170
+ bc [ i] [ 0 ] = 1
171
+ bc [ i] [ i] = 1
178
172
}
179
- }
180
-
181
- return bc [ n, k]
173
+
174
+ if n > 0 {
175
+ for i in 1 ... n {
176
+ for j in 1 ..< i {
177
+ bc [ i] [ j] = bc [ i - 1 ] [ j - 1 ] + bc[ i - 1 ] [ j]
178
+ }
179
+ }
180
+ }
181
+
182
+ return bc [ n] [ k]
182
183
}
183
184
184
185
binomialCoefficient ( 30 , 15 )
0 commit comments