123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // Binomial coefficient
- //
- // Input: tos-2 n
- //
- // tos-1 k
- //
- // Output: Binomial coefficient on stack
- //
- // binomial(n, k) = n! / k! / (n - k)!
- //
- // The binomial coefficient vanishes for k < 0 or k > n. (A=B, p. 19)
- #include "stdafx.h"
- #include "defs.h"
- static void ybinomial(void);
- static int check_args(void);
- void
- eval_binomial(void)
- {
- push(cadr(p1));
- eval();
- push(caddr(p1));
- eval();
- binomial();
- }
- void
- binomial(void)
- {
- save();
- ybinomial();
- restore();
- }
- #define N p1
- #define K p2
- static void
- ybinomial(void)
- {
- K = pop();
- N = pop();
- if (check_args() == 0) {
- push(zero);
- return;
- }
- push(N);
- factorial();
- push(K);
- factorial();
- divide();
- push(N);
- push(K);
- subtract();
- factorial();
- divide();
- }
- static int
- check_args(void)
- {
- if (isnum(N) && lessp(N, zero))
- return 0;
- else if (isnum(K) && lessp(K, zero))
- return 0;
- else if (isnum(N) && isnum(K) && lessp(N, K))
- return 0;
- else
- return 1;
- }
|