Evaluate binomial coefficients: Difference between revisions

(→‎{{header|dc}}: Add commented version.)
Line 367:
 
<lang dc>[ macro z: factorial base case when n is (z)ero ]sx
[sx [ x is our dump register; storingget thererid isof equivalentextraneous copy of dropn we no longer need]sx
1 [ return value is 1 ]sx
q] [ abort processing of calling macro ]sx
Line 373:
 
[ macro f: factorial ]sx [
d [ duplicate the input (n) ]sx
0 =z [ if it'sn is zero, call z, which stops here and returns 1 ]sx
d [ otherwise, duplicate n again ]sx
1 - [ subtract 1 ]sx
Line 381:
] sf
 
[ macro b(n,k): binomial function (n choose k)]sx. [
straightforward RPN version of formula.]sx [
sk [ remember k ]sx
d sk [ nremember k. stack: n ]sx
lfxd [ duplicate: n n! ]sx
rlfx [ call factorial: [ n! n! ]sx
lkr [ swap: n! n k ]sx
- lk [ load k: n! n- k ]sx
lfx- [ subtract: n! ( n-k)! ]sx
lk lfx [ call factorial: n! (n-k)! k ]sx
lfxlk [ load k: n! (n-k)! k! ]sx
*lfx [ call factorial; [ n! (n-k)! k! ]sx
/* [ multiply: n!/ (n-k)!k! ]sx
/ [ divide: n!/(n-k)!k! ]sx
] sb
 
1,481

edits