Combinations and permutations: Difference between revisions

Content added Content deleted
(→‎{{header|jq}}: add details about gojq)
(→‎{{header|Bracmat}}: Added floating point implementations of C and P)
Line 322: Line 322:


=={{header|Bracmat}}==
=={{header|Bracmat}}==
Bracmat cannot handle floating point numbers. Instead, this solution shows the first 50 digits and a count of the digits that are not shown.
This solution shows results from bignum as well as floating point implementations. If bignum answers are very big, only the first 50 digits and a count of the digits that are not shown are shown. The bignum answers are shown as Cn and Pn, while the floating point answers are shown as Cf and Pf. Answers are aligned to make it easy to compare the decimals.
<syntaxhighlight lang="bracmat">( ( C
<syntaxhighlight lang="bracmat"> ( C
= n k coef
= n k coef
. !arg:(?n,?k)
. !arg:(?n,?k)
Line 336: Line 336:
& !coef
& !coef
)
)
& ( compileBinomialFunctionThatDoesFloatingPointCalculations
=
. new
$ ( UFP
,
' ( (s.n) (s.k)
. "**************************************************************
*** Notice the difference between the following four lines ***
*** of code and the much shorter (!n+-1*!k:<!k:?k|) in ***
*** function C above. UFP grammar is simpler than usual ***
*** Bracmat grammar. UFP code is therefore less terse. ***
**************************************************************"
& !n+-1*!k:?n-k
& ( !n-k:<!k&!n-k:?k
|
)
& 1:?coef
& whl
' ( !k:>0
& !coef*!n*!k^-1:?coef
& !k+-1:?k
& !n+-1:?n
)
& !coef
)
)
)
& compileBinomialFunctionThatDoesFloatingPointCalculations$
: ?binom
& ( P
& ( P
= n k result
= n k result
Line 348: Line 377:
& !result
& !result
)
)
& ( compilePermutationFunctionThatDoesFloatingPointCalculations
=
. new
$ ( UFP
,
' ( (s.n) (s.k)
. !n+-1*!k:?k
& 1:?result
& whl
' ( !n:>!k
& !n*!result:?result
& !n+-1:?n
)
& !result
)
)
)
& compilePermutationFunctionThatDoesFloatingPointCalculations$
: ?permu
& 0:?i
& 0:?i
& whl
& whl
Line 358: Line 406:
' ( 10+!i:~>60:?i
' ( 10+!i:~>60:?i
& div$(!i.3):?k
& div$(!i.3):?k
& out$(!i C !k "=" C$(!i,!k))
& out$(!i Cn !k "= " C$(!i,!k))
& out$(!i Cf !k "=" (binom..go)$(!i,!k))
)
)
& ( displayBig
& ( displayBig
Line 370: Line 419:
' ( !is:%?i ?is
' ( !is:%?i ?is
& div$(!i.3):?k
& div$(!i.3):?k
& out
& out$(str$(!i " P " !k " = " displayBig$(P$(!i,!k))))
$ ( str
$ (!i " Pn " !k " = " displayBig$(P$(!i,!k)))
)
& out
$ ( str
$ (!i " Pf " !k " = " (permu..go)$(!i,!k))
)
)
)
& 0:?i
& 0:?i
Line 376: Line 432:
' ( 100+!i:~>1000:?i
' ( 100+!i:~>1000:?i
& div$(!i.3):?k
& div$(!i.3):?k
& out
& out$(str$(!i " C " !k " = " displayBig$(C$(!i,!k))))
$ ( str
$ (!i " Cn " !k " = " displayBig$(C$(!i,!k)))
)
& out
$ ( str
$ (!i " Cf " !k " = " (binom..go)$(!i,!k))
)
)
)
);</syntaxhighlight>
& all done;</syntaxhighlight>
Output:
Output:
<pre>1 P 0 = 1
<pre>1 P 0 = 1
Line 392: Line 455:
11 P 3 = 990
11 P 3 = 990
12 P 4 = 11880
12 P 4 = 11880
10 C 3 = 120
10 Cn 3 = 120
10 Cf 3 = 1.1999999999999999E+02
20 C 6 = 38760
30 C 10 = 30045015
20 Cn 6 = 38760
20 Cf 6 = 3.8759999999999993E+04
40 C 13 = 12033222880
50 C 16 = 4923689695575
30 Cn 10 = 30045015
30 Cf 10 = 3.0045014999999989E+07
60 C 20 = 4191844505805495
5 P 1 = 5
40 Cn 13 = 12033222880
40 Cf 13 = 1.2033222880000000E+10
50 P 16 = 103017324974226408345600000
50 Cn 16 = 4923689695575
500 P 166 = 35348749217429427876093618266017623068440028791060... (385 more digits)
50 Cf 16 = 4.9236896955750000E+12
1000 P 333 = 59693262885034150890397017659007842809981894765670... (922 more digits)
60 Cn 20 = 4191844505805495
5000 P 1666 = 68567457572556742754845369402488960622341567102448... (5976 more digits)
60 Cf 20 = 4.1918445058054930E+15
15000 P 5000 = 96498539887274939220148588059312959807922816886808... (20420 more digits)
5 Pn 1 = 5
100 C 33 = 294692427022540894366527900
5 Pf 1 = 5.0000000000000000E+00
200 C 66 = 72697525451692783415270666511927389767550269141935... (4 more digits)
50 Pn 16 = 103017324974226408345600000
300 C 100 = 41582514632585647447833835263264055802804660057436... (32 more digits)
50 Pf 16 = 1.0301732497422640E+26
400 C 133 = 12579486841821087021333484756519650044917494358375... (60 more digits)
500 C 166 = 39260283861944227552204083450723314281973490135301... (87 more digits)
500 Pn 166 = 35348749217429427876093618266017623068440028791060... (385 more digits)
500 Pf 166 = INF
600 C 200 = 25060177832214028050056167705132288352025510250879... (115 more digits)
700 C 233 = 81032035633395999047404536440311382329449203119421... (142 more digits)
1000 Pn 333 = 59693262885034150890397017659007842809981894765670... (922 more digits)
1000 Pf 333 = INF
800 C 266 = 26456233626836270342888292995561242550915240450150... (170 more digits)
900 C 300 = 17433563732964466429607307650857183476303419689548... (198 more digits)
5000 Pn 1666 = 68567457572556742754845369402488960622341567102448... (5976 more digits)
5000 Pf 1666 = INF
1000 C 333 = 57761345531476516697774863235496017223394580195002... (225 more digits)</pre>
15000 Pn 5000 = 96498539887274939220148588059312959807922816886808... (20420 more digits)
15000 Pf 5000 = INF
100 Cn 33 = 294692427022540894366527900
100 Cf 33 = 2.9469242702254079E+26
200 Cn 66 = 72697525451692783415270666511927389767550269141935... (4 more digits)
200 Cf 66 = 7.2697525451692816E+53
300 Cn 100 = 41582514632585647447833835263264055802804660057436... (32 more digits)
300 Cf 100 = 4.1582514632585569E+81
400 Cn 133 = 12579486841821087021333484756519650044917494358375... (60 more digits)
400 Cf 133 = 1.2579486841821091E+109
500 Cn 166 = 39260283861944227552204083450723314281973490135301... (87 more digits)
500 Cf 166 = 3.9260283861944195E+136
600 Cn 200 = 25060177832214028050056167705132288352025510250879... (115 more digits)
600 Cf 200 = 2.5060177832213946E+164
700 Cn 233 = 81032035633395999047404536440311382329449203119421... (142 more digits)
700 Cf 233 = 8.1032035633395859E+191
800 Cn 266 = 26456233626836270342888292995561242550915240450150... (170 more digits)
800 Cf 266 = 2.6456233626836295E+219
900 Cn 300 = 17433563732964466429607307650857183476303419689548... (198 more digits)
900 Cf 300 = 1.7433563732964451E+247
1000 Cn 333 = 57761345531476516697774863235496017223394580195002... (225 more digits)
1000 Cf 333 = 5.7761345531476355E+274
{!} all done</pre>


=={{header|C}}==
=={{header|C}}==