Evaluate binomial coefficients: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments, changed output, added whitespace, added subroutine fences. -- ~~~~)
Line 1,046: Line 1,046:
=={{header|REXX}}==
=={{header|REXX}}==
The task is to compute ANY binomial coefficient(s), but this example is limited to 100k digits.
The task is to compute ANY binomial coefficient(s), but this example is limited to 100k digits.
<lang rexx>/*REXX program calculates binomial coefficients (aka, combinations). */
<lang rexx>/*REXX program calculates binomial coefficients (aka, combinations). */

numeric digits 100000
numeric digits 100000
parse arg n k .
parse arg n k .
say 'comb('n","k')='comb(n,k)
say 'combinations('n","k') =' comb(n,k)
exit /*stick a fork in it, we're done.*/
exit

/*──────────────────────────────────COMB subroutine─────────────────────*/
comb: procedure; parse arg x,y; return fact(x) / (fact(x-y) * fact(y))


/*──────────────────────────────────FACT subroutine─────────────────────*/
comb: procedure; parse arg x,y; return fact(x)/(fact(x-y)*fact(y))
fact: procedure; parse arg z; !=1; do j=2 to z; !=!*j; end; return !</lang>
fact: procedure; parse arg z; !=1; do j=2 to z; !=!*j; end; return !</lang>
Output when using the input of:
'''output''' when using the input of: <tt> 5 3 </tt>
<pre>5 3</pre>
<pre>
<pre>
comb(5,3)=10
combinations(5,3) = 10
</pre>
</pre>
Output when using the input of:
'''output''' when using the input of: <tt> 1200 120 </tt>
<pre style="overflow:scroll">
<pre>1200 120</pre>
combinations(1200,120) = 1004576581793084916475353119318331966507299414258370667602185866686463289093457468590558508056798211449853806741873396451735444387513582540860551330127062642417424083600
<pre style="height:5ex;overflow:scroll">
comb(1200,120)=1004576581793084916475353119318331966507299414258370667602185866686463289093457468590558508056798211449853806741873396451735444387513582540860551330127062642417424083600
</pre>
</pre>