[system] / trunk / pg / macros / contextIntegerFunctions.pl Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

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

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

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

 aubreyja at gmail dot com ViewVC Help Powered by ViewVC 1.0.9