Combinations and permutations: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 967:
329.0 ⊞ 34.0 = 2.225630e+46
464.0 ⊞ 178.0 = 5.615925e+132
</pre>
 
=={{header|Kotlin}}==
As Kotlin/JVM can use the java.math.BigInteger class, there is no need to use floating point approximations and so we use exact integer arithmetic for all parts of the task.
<lang scala>// version 1.1.2
 
import java.math.BigInteger
 
fun perm(n: Int, k: Int): BigInteger {
require(n > 0 && k >= 0)
return (n - k + 1 .. n).fold(BigInteger.ONE) { acc, i -> acc * BigInteger.valueOf(i.toLong()) }
}
 
fun comb(n: Int, k: Int): BigInteger {
require(n > 0 && k >= 0)
val fact = (2..k).fold(BigInteger.ONE) { acc, i -> acc * BigInteger.valueOf(i.toLong()) }
return perm(n, k) / fact
}
 
fun main(args: Array<String>) {
println("A sample of permutations from 1 to 12:")
for (n in 1..12) System.out.printf("%2d P %-2d = %d\n", n, n / 3, perm(n, n / 3))
 
println("\nA sample of combinations from 10 to 60:")
for (n in 10..60 step 10) System.out.printf("%2d C %-2d = %d\n", n, n / 3, comb(n, n / 3))
 
println("\nA sample of permutations from 5 to 15000:")
val na = intArrayOf(5, 50, 500, 1000, 5000, 15000)
for (n in na) {
val k = n / 3
val s = perm(n, k).toString()
val l = s.length
val e = if (l <= 40) "" else "... (${l - 40} more digits)"
System.out.printf("%5d P %-4d = %s%s\n", n, k, s.take(40), e)
}
 
println("\nA sample of combinations from 100 to 1000:")
for (n in 100..1000 step 100) {
val k = n / 3
val s = comb(n, k).toString()
val l = s.length
val e = if (l <= 40) "" else "... (${l - 40} more digits)"
System.out.printf("%4d C %-3d = %s%s\n", n, k, s.take(40), e)
}
}</lang>
 
{{out}}
<pre>
A 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
 
A 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
 
A sample of permutations from 5 to 15000:
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)
 
A 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>
 
9,492

edits