[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 6058 - (view) (download) (as text)

1 : sh002i 5556 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4 : gage 6058 # $CVSHeader$
5 : sh002i 5556 #
6 :     # This program is free software; you can redistribute it and/or modify it under
7 :     # the terms of either: (a) the GNU General Public License as published by the
8 :     # Free Software Foundation; either version 2, or (at your option) any later
9 :     # version, or (b) the "Artistic License" which comes with this package.
10 :     #
11 :     # This program is distributed in the hope that it will be useful, but WITHOUT
12 :     # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 :     # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 :     # Artistic License for more details.
15 :     ################################################################################
16 :    
17 : sh002i 5551 =head1 NAME
18 : jj 3305
19 : sh002i 5551 contextIntegerFunctions.pl - adds integer related functions C(n,r) and P(n,r).
20 : jj 3305
21 : sh002i 5551 =head1 DESCRIPTION
22 : gage 4997
23 : sh002i 5551 This is a Parser context that adds integer related functions C(n,r)
24 :     and P(n,r). They can be used by the problem author and also by
25 :     students if the answer checking is done by Parser. The latter is
26 :     the main purpose of this file.
27 : jj 3305
28 : sh002i 5551 B<Note:> by default, webwork problems do not permit students to use
29 :     C(n,r) and P(n,r) functions. Problems which do permit this
30 :     should alert the student in their text.
31 :    
32 :     =head1 USAGE
33 :    
34 : sh002i 5555 Context("IntegerFunctions")
35 :    
36 :     $b = random(2, 5); $a = $b+random(0, 5);
37 :     $c = C($a, $b);
38 :     ANS(Compute("P($a, $b)")->cmp);
39 : sh002i 5551
40 :     B<Note:> If the context is set to something else, such as Numeric, it
41 :     can be set back with Context("IntegerFunctions").
42 :    
43 : gage 4997 =cut
44 : jj 3305
45 : sh002i 5551 loadMacros('MathObjects.pl');
46 :    
47 :     sub _contextIntegerFunctions_init {context::IntegerFunctions2::Init()}; # don't reload this file
48 :    
49 : gage 5843 package context::IntegerFunctions2;
50 : jj 3305 our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs
51 :    
52 :     sub C {
53 :     shift; my ($n,$r) = @_; my $C = 1;
54 :     return (0) if($r>$n);
55 :     $r = $n-$r if ($r > $n-$r); # find the smaller of the two
56 :     for (1..$r) {$C = ($C*($n-$_+1))/$_}
57 :     return $C
58 :     }
59 :    
60 :     sub P {
61 :     shift; my ($n,$r) = @_; my $P = 1;
62 :     return (0) if($r>$n);
63 :     for (1..$r) {$P *= ($n-$_+1)}
64 :     return $P
65 :     }
66 :    
67 : dpvc 5392 sub Init {
68 :     my $context = $main::context{IntegerFunctions} = Parser::Context->getCopy("Numeric");
69 : dpvc 5441 $context->{name} = "IntegerFunctions";
70 : jj 3305
71 : dpvc 5392 $context->functions->add(
72 : gage 5843 C => {class => 'context::IntegerFunctions2'},
73 :     P => {class => 'context::IntegerFunctions2'},
74 : dpvc 5392 );
75 : jj 3305
76 : dpvc 5392 main::Context("IntegerFunctions");
77 :     }
78 : jj 3305
79 : dpvc 5373 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9