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

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

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

 1 : sh002i 5551 =head1 NAME 2 : jj 3305 3 : sh002i 5551 contextIntegerFunctions.pl - adds integer related functions C(n,r) and P(n,r). 4 : jj 3305 5 : sh002i 5551 =head1 DESCRIPTION 6 : gage 4997 7 : sh002i 5551 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 : jj 3305 12 : sh002i 5551 B 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 : =head1 USAGE 17 : 18 : Context("IntegerFunctions") 19 : 20 : \$b = random(2, 5); \$a = \$b+random(0, 5); 21 : \$c = C(\$a, \$b); 22 : ANS(Compute("P(\$a, \$b)")->cmp); 23 : 24 : B If the context is set to something else, such as Numeric, it 25 : can be set back with Context("IntegerFunctions"). 26 : 27 : gage 4997 =cut 28 : jj 3305 29 : sh002i 5551 loadMacros('MathObjects.pl'); 30 : 31 : sub _contextIntegerFunctions_init {context::IntegerFunctions2::Init()}; # don't reload this file 32 : 33 : dpvc 5392 package context::IntegerFunction2; 34 : jj 3305 our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs 35 : 36 : sub C { 37 : shift; my (\$n,\$r) = @_; my \$C = 1; 38 : return (0) if(\$r>\$n); 39 : \$r = \$n-\$r if (\$r > \$n-\$r); # find the smaller of the two 40 : for (1..\$r) {\$C = (\$C*(\$n-\$_+1))/\$_} 41 : return \$C 42 : } 43 : 44 : sub P { 45 : shift; my (\$n,\$r) = @_; my \$P = 1; 46 : return (0) if(\$r>\$n); 47 : for (1..\$r) {\$P *= (\$n-\$_+1)} 48 : return \$P 49 : } 50 : 51 : dpvc 5392 sub Init { 52 : my \$context = \$main::context{IntegerFunctions} = Parser::Context->getCopy("Numeric"); 53 : dpvc 5441 \$context->{name} = "IntegerFunctions"; 54 : jj 3305 55 : dpvc 5392 \$context->functions->add( 56 : C => {class => 'context::IntegerFunction2'}, 57 : P => {class => 'context::IntegerFunction2'}, 58 : ); 59 : jj 3305 60 : dpvc 5392 main::Context("IntegerFunctions"); 61 : } 62 : jj 3305 63 : dpvc 5373 1;

 aubreyja at gmail dot com ViewVC Help Powered by ViewVC 1.0.9