[system] / trunk / pg / macros / contextIntegerFunctions.pl Repository:
ViewVC logotype

Annotation of /trunk/pg/macros/contextIntegerFunctions.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3305 - (view) (download) (as text)

1 : jj 3305 loadMacros('Parser.pl');
2 :    
3 :     sub _contextIntegerFunctions_init {}; # don't reload this file
4 :    
5 :     ######################################################################
6 :     #
7 :     # This is a Parser context that adds integer related functions C(n,r)
8 :     # and P(n,r). They can be used by the problem author and also by
9 :     # students if the answer checking is done by Parser. The latter is
10 :     # the main purpose of this file.
11 :     #
12 :     # Note: by default, webwork problems do not permit students to use
13 :     # C(n,r) and P(n,r) functions. Problems which do permit this
14 :     # should alert the student in their text.
15 :     #
16 :     # Usage examples:
17 :     # $b = random(2, 5); $a = $b+random(0, 5);
18 :     # $c = C($a, $b);
19 :     # ANS(Compute("P($a, $b)")->cmp);
20 :     #
21 :     # Note: If the context is set to something else, such as Numeric, it
22 :     # can be set back with Context("IntegerFunctions").
23 :    
24 :    
25 :     $context{IntegerFunctions} = Context("Numeric")->copy;
26 :    
27 :     package IntegerFunction2;
28 :     our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs
29 :    
30 :     sub C {
31 :     shift; my ($n,$r) = @_; my $C = 1;
32 :     return (0) if($r>$n);
33 :     $r = $n-$r if ($r > $n-$r); # find the smaller of the two
34 :     for (1..$r) {$C = ($C*($n-$_+1))/$_}
35 :     return $C
36 :     }
37 :    
38 :     sub P {
39 :     shift; my ($n,$r) = @_; my $P = 1;
40 :     return (0) if($r>$n);
41 :     for (1..$r) {$P *= ($n-$_+1)}
42 :     return $P
43 :     }
44 :    
45 :     package main;
46 :    
47 :     $context{'IntegerFunctions'}->functions->add(
48 :     C => {class => 'IntegerFunction2'},
49 :     P => {class => 'IntegerFunction2'},
50 :     );
51 :    
52 :     Context("IntegerFunctions");
53 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9