-
Notifications
You must be signed in to change notification settings - Fork 59
/
choose.coffee
76 lines (54 loc) · 1.06 KB
/
choose.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
### choose =====================================================================
Tags
----
scripting, JS, internal, treenode, general concept
Parameters
----------
n,k
General description
-------------------
Returns the number of combinations of n items taken k at a time.
For example, the number of five card hands is choose(52,5)
```
n!
choose(n,k) = -------------
k! (n - k)!
```
###
Eval_choose = ->
push(cadr(p1))
Eval()
push(caddr(p1))
Eval()
choose()
# Result vanishes for k < 0 or k > n. (A=B, p. 19)
#define N p1
#define K p2
choose = ->
save()
p2 = pop()
p1 = pop()
if (choose_check_args() == 0)
push_integer(0)
restore()
return
push(p1)
factorial()
push(p2)
factorial()
divide()
push(p1)
push(p2)
subtract()
factorial()
divide()
restore()
choose_check_args = ->
if (isNumericAtom(p1) && lessp(p1, zero))
return 0
else if (isNumericAtom(p2) && lessp(p2, zero))
return 0
else if (isNumericAtom(p1) && isNumericAtom(p2) && lessp(p1, p2))
return 0
else
return 1