Combinations and permutations: Difference between revisions

Added Wren
(Combinations and permutations en FreeBasic)
(Added Wren)
Line 2,712:
--
C(5000,4000)=57462357505803375604893834658665168251899919793850512934468881710397678593302188064618445132583370701755893065787216750992391223467601994741594656878559929037277303674963658032197224327768110236651567704673226756781828332650887849150208195780031161286578505113618731045004523840401144118298192191997565735245181433457469532981432785237769191864102953974244072964471109551273603780184330987071947790993108191904370472373403157802158903129815170101708451875442019845175637901995588390614304812103202403626211504997668649346891167495657556154392183988627948442807346603688457854135114491955258804187129028256547543888109987151649038111791932035229202856007767332717845596528598314477979861265222941138323298702349967224867703420888363395662988291273283611081068577160905840445308086112429900453394212790633910614322699210850302387512579976209123523546689147207974269396548873838155355768985614160932799226261509866933143702889005270480654844312094564956178277998090168826124850606847021667494019587077659107276117413835912767949017954979839258481340540145909025953956582025656306426226560
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-trait}}
<lang ecmascript>import "/big" for BigInt
import "/fmt" for Fmt
import "/trait" for Stepped
 
var perm = Fn.new { |n, k|
if (n <= 0 || k < 0) Fiber.abort("Invalid argument(s).")
if (k == 0) return BigInt.one
return (n-k+1..n).reduce(BigInt.one) { |acc, i| acc * BigInt.new(i) }
}
 
var comb = Fn.new { |n, k|
if (n <= 0 || k < 0) Fiber.abort("Invalid argument(s).")
var fact = BigInt.one
if (k > 1) fact = (2..k).reduce(BigInt.one) { |acc, i| acc * BigInt.new(i) }
return perm.call(n, k) / fact
}
 
System.print("A sample of permutations from 1 to 12:")
for (n in 1..12) Fmt.print("$2d P $-2d = $i", n, (n/3).floor, perm.call(n, (n/3).floor))
 
System.print("\nA sample of combinations from 10 to 60:")
for (n in Stepped.new(10..60, 10)) {
Fmt.print("$2d C $-2d = $i", n, (n/3).floor, comb.call(n, (n/3).floor))
}
 
System.print("\nA sample of permutations from 5 to 15000:")
var na = [5, 50, 500, 1000, 5000, 15000]
for (n in na) {
var k = (n/3).floor
var s = perm.call(n, k).toString
var l = s.count
var e = (l <= 40) ? "" : "... (%(l - 40) more digits)"
Fmt.print("$5d P $-4d = $s$s", n, k, s.take(40).join(), e)
}
 
System.print("\nA sample of combinations from 100 to 1000:")
for (n in Stepped.new(100..1000, 100)) {
var k = (n/3).floor
var s = comb.call(n, k).toString
var l = s.count
var e = (l <= 40) ? "" : "... (%(l - 40) more digits)"
Fmt.print("$4d C $-3d = $s$s", n, k, s.take(40).join(), 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,485

edits