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

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

Tue Aug 28 22:40:15 2007 UTC (12 years, 5 months ago) by dpvc
File size: 1675 byte(s)
```Add context names for the context(s) created here.
```

```    1 loadMacros('MathObjects.pl');
2
3 sub _contextIntegerFunctions_init {context::IntegerFunctions2::Init()}; # don't reload this file
4
6
7  ######################################################################
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
26 =cut
27
28 package context::IntegerFunction2;
29 our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs
30
31 sub C {
32   shift; my (\$n,\$r) = @_; my \$C = 1;
33   return (0) if(\$r>\$n);
34   \$r = \$n-\$r if (\$r > \$n-\$r); # find the smaller of the two
35   for (1..\$r) {\$C = (\$C*(\$n-\$_+1))/\$_}
36   return \$C
37 }
38
39 sub P {
40   shift; my (\$n,\$r) = @_; my \$P = 1;
41   return (0) if(\$r>\$n);
42   for (1..\$r) {\$P *= (\$n-\$_+1)}
43   return \$P
44 }
45
46 sub Init {
47   my \$context = \$main::context{IntegerFunctions} = Parser::Context->getCopy("Numeric");
48   \$context->{name} = "IntegerFunctions";
49