| … | |
… | |
| 32 | |
32 | |
| 33 | # |
33 | # |
| 34 | # Compute the value of n choose r. |
34 | # Compute the value of n choose r. |
| 35 | # |
35 | # |
| 36 | sub _eval { |
36 | sub _eval { |
| 37 | shift; my ($n,$r) = @_; |
37 | shift; my ($n,$r) = @_; my $C = 1; |
| 38 | $r = $n-$r if ($r > $n - $r); # find the smaller of the two |
38 | $r = $n-$r if ($r > $n-$r); # find the smaller of the two |
| 39 | my $m = $n - $r; my $C = 1; |
39 | for (1..$r) {$C = $C*($n-$_+1)/$_} |
| 40 | # |
|
|
| 41 | # We compute this so that the value never gets too large |
|
|
| 42 | # |
|
|
| 43 | while ($n > $m) {$C = $C*$n/$r; $n--; $r-- if $r > 1} |
|
|
| 44 | while ($r > 1) {$C /= $r; $r--} |
|
|
| 45 | return $C |
40 | return $C |
| 46 | } |
41 | } |
| 47 | |
42 | |
| 48 | # |
43 | # |
| 49 | # Non-standard TeX output |
44 | # Non-standard TeX output |
| … | |
… | |
| 71 | $prec = Context()->operators->get('+')->{precedence} - .25; |
66 | $prec = Context()->operators->get('+')->{precedence} - .25; |
| 72 | |
67 | |
| 73 | Context()->operators->add( |
68 | Context()->operators->add( |
| 74 | '#' => { |
69 | '#' => { |
| 75 | class => 'MyOperator', |
70 | class => 'MyOperator', |
| 76 | precedence => $prec, # just below addition |
71 | precedence => $prec, # just below addition |
| 77 | associativity => 'left', # computed left to right |
72 | associativity => 'left', # computed left to right |
| 78 | type => 'bin', # binary operator |
73 | type => 'bin', # binary operator |
| 79 | string => ' # ', # output string for it |
74 | string => ' # ', # output string for it |
| 80 | TeX => '\mathop{#}', # TeX version (overridden above, but just an example) |
75 | TeX => '\mathop{#}', # TeX version (overridden above, but just an example) |
| 81 | } |
76 | } |
| 82 | ); |
77 | ); |
| 83 | |
78 | |
| 84 | |
79 | |
| 85 | # |
80 | # |
| … | |
… | |
| 91 | # |
86 | # |
| 92 | # The problem text |
87 | # The problem text |
| 93 | # |
88 | # |
| 94 | BEGIN_TEXT |
89 | BEGIN_TEXT |
| 95 | |
90 | |
| 96 | In this problem, we have added a new operator to the Parser: ${BTT} n # r${ETT}, |
91 | In this problem, we have added a new operator to the Parser: ${BTT}n # r${ETT}, |
| 97 | which returns \(n\choose r\). |
92 | which returns \(n\choose r\). |
| 98 | $PAR |
93 | $PAR |
| 99 | |
94 | |
| 100 | \{ParserTable( |
95 | \{ParserTable( |
| 101 | 'Formula("x # y")', |
96 | 'Formula("x # y")', |