Combinations and permutations: Difference between revisions
Content added Content deleted
No edit summary |
(Add Swift) |
||
Line 1,990: | Line 1,990: | ||
return(exp(lnfactorial(n)-lnfactorial(n-k))) |
return(exp(lnfactorial(n)-lnfactorial(n-k))) |
||
}</lang> |
}</lang> |
||
=={{header|Swift}}== |
|||
{{trans|Kotlin}} |
|||
Using AttaSwift's BigInt |
|||
<lang swift>import BigInt |
|||
func permutations(n: Int, k: Int) -> BigInt { |
|||
let l = n - k + 1 |
|||
guard l <= n else { |
|||
return 1 |
|||
} |
|||
return (l...n).reduce(BigInt(1), { $0 * BigInt($1) }) |
|||
} |
|||
func combinations(n: Int, k: Int) -> BigInt { |
|||
let fact = {() -> BigInt in |
|||
guard k > 1 else { |
|||
return 1 |
|||
} |
|||
return (2...k).map({ BigInt($0) }).reduce(1, *) |
|||
}() |
|||
return permutations(n: n, k: k) / fact |
|||
} |
|||
print("Sample of permutations from 1 to 12") |
|||
for i in 1...12 { |
|||
print("\(i) P \(i / 3) = \(permutations(n: i, k: i / 3))") |
|||
} |
|||
print("\nSample of combinations from 10 to 60") |
|||
for i in stride(from: 10, through: 60, by: 10) { |
|||
print("\(i) C \(i / 3) = \(combinations(n: i, k: i / 3))") |
|||
} |
|||
print("\nSample of permutations from 5 to 15,000") |
|||
for i in [5, 50, 500, 1000, 5000, 15000] { |
|||
let k = i / 3 |
|||
let res = permutations(n: i, k: k).description |
|||
let extra = res.count > 40 ? "... (\(res.count - 40) more digits)" : "" |
|||
print("\(i) P \(k) = \(res.prefix(40))\(extra)") |
|||
} |
|||
print("\nSample of combinations from 100 to 1000") |
|||
for i in stride(from: 100, through: 1000, by: 100) { |
|||
let k = i / 3 |
|||
let res = combinations(n: i, k: k).description |
|||
let extra = res.count > 40 ? "... (\(res.count - 40) more digits)" : "" |
|||
print("\(i) C \(k) = \(res.prefix(40))\(extra)") |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Sample of permutations from 1 to 12 |
|||
1 P 0 = 1 |
|||
2 P 0 = 1 |
|||
3 P 1 = 3 |
|||
4 P 1 = 4 |
|||
5 P 1 = 5 |
|||
6 P 2 = 30 |
|||
7 P 2 = 42 |
|||
8 P 2 = 56 |
|||
9 P 3 = 504 |
|||
10 P 3 = 720 |
|||
11 P 3 = 990 |
|||
12 P 4 = 11880 |
|||
Sample of combinations from 10 to 60 |
|||
10 C 3 = 120 |
|||
20 C 6 = 38760 |
|||
30 C 10 = 30045015 |
|||
40 C 13 = 12033222880 |
|||
50 C 16 = 4923689695575 |
|||
60 C 20 = 4191844505805495 |
|||
Sample of permutations from 5 to 15,000 |
|||
5 P 1 = 5 |
|||
50 P 16 = 103017324974226408345600000 |
|||
500 P 166 = 3534874921742942787609361826601762306844... (395 more digits) |
|||
1000 P 333 = 5969326288503415089039701765900784280998... (932 more digits) |
|||
5000 P 1666 = 6856745757255674275484536940248896062234... (5986 more digits) |
|||
15000 P 5000 = 9649853988727493922014858805931295980792... (20430 more digits) |
|||
Sample of combinations from 100 to 1000 |
|||
100 C 33 = 294692427022540894366527900 |
|||
200 C 66 = 7269752545169278341527066651192738976755... (14 more digits) |
|||
300 C 100 = 4158251463258564744783383526326405580280... (42 more digits) |
|||
400 C 133 = 1257948684182108702133348475651965004491... (70 more digits) |
|||
500 C 166 = 3926028386194422755220408345072331428197... (97 more digits) |
|||
600 C 200 = 2506017783221402805005616770513228835202... (125 more digits) |
|||
700 C 233 = 8103203563339599904740453644031138232944... (152 more digits) |
|||
800 C 266 = 2645623362683627034288829299556124255091... (180 more digits) |
|||
900 C 300 = 1743356373296446642960730765085718347630... (208 more digits) |
|||
1000 C 333 = 5776134553147651669777486323549601722339... (235 more digits)</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |