Combinations and permutations: Difference between revisions

→‎{{header|jq}}: add details about gojq
(→‎{{header|RPL}}: floating point aprox)
(→‎{{header|jq}}: add details about gojq)
Line 1,339:
 
=={{header|jq}}==
Currently,The C implementation of jq approximates large integers by IEEE 754 64-bit floats, and only supports tgamma (true gamma). Thus beyond about 1e308 all accuracy is lost.
and only supports tgamma (true gamma), and thus beyond about 1e308 all accuracy is lost.
<syntaxhighlight lang="jq">def permutation(k): . as $n
 
On the other hand, gojq, the Go implementation, supports
unbounded-precision integer arithmetic,
and the `permutation` and `combination` functions below are
implemented so as to preserve precision.
<syntaxhighlight lang="jq">def permutation(k): . as $n
def permutation(k): . as $n
| reduce range($n-k+1; 1+$n) as $i (1; . * $i);
Line 1,359 ⟶ 1,366:
def big_permutation(k): log_permutation(k) | exp;
 
def big_combination(k): log_combination(k) | exp;</syntaxhighlight>
</syntaxhighlight>
'''Examples''':
'''Examples using the C implementation''':
12 | permutation(9) #=> 79833600
<pre>
12 | permutation(9) #=> 79833600
 
17012 | big_permutation(1339) #=> 579833599.272846415658284e+26399999964
 
1260 | big_permutationcombination(953) #=> 79833599.99999964386206920
 
60 | combinationbig_combination(53) #=> 386206920.0000046
 
60145 | big_combinationbig_permutation(53133) #=> 3862069201.00000466801459655817956e+243
 
145170 | big_permutation(133) #=> 15.6801459655817e272846415658284e+243263
</pre>
'''Examples using gojq, the Go implementation'''
<pre>
60 | combinations(30) #=> 118264581564861424
 
1000 | combination(333) | tostring | "\(.[:40]) ... (\(length) digits in all)"
170 | big_permutation(133) #=> 5.272846415658284e+263
#=> 5776134553147651669777486323549601722339 ... (275 digits in all)
</pre>
 
=={{header|Julia}}==
2,442

edits