WeBWorK Problems

Using binomial coefficient notation C(n,r) in answers

Using binomial coefficient notation C(n,r) in answers

by Ron Graham -
Number of replies: 3
I would like to create problems where the answer involves binomial coefficients with variable arguments .For example, the sum of the first n 
positive integers is n*(n+1)/2.
I would like to compose the problem (and have a correct student answer)
in the form C(n+1,2). I tried the following but (of course!) it doesn't work:

DOCUMENT();        

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl",
"contextIntegerFunctions.pl",
);

TEXT(&beginproblem);
Context("IntegerFunctions");


BEGIN_TEXT
What is an explicit expression for the sum
of the first $n positive integers?
$BR


\( a_n = \) \{ ans_rule(40) \}

$PAR
END_TEXT

ANS( fun_cmp( "n*(n+1)/2", var => 'n', test_points => [random(1,50,2), random(1,50,2), random(2,50,2), random(2,50,2), random(1,50)] ) );


ENDDOCUMENT();        

Is there a way to do this?

Ron Graham

In reply to Ron Graham

Re: Using binomial coefficient notation C(n,r) in answers

by Michael Gage -
Hi.  

The main problem is using fun_cmp instead of the MathObject syntax.  fun_cmp is an earlier (non-object oriented) version of calling answer evaluators.   It is currently implemented by actually creating a Formula MathObject and then using its compare (cmp) method to evaluate the student's answers.  Unfortunately it creates this Formula MathObject using the default 
context "Numeric" which doesn't allow the function C(n,k). Hence the error message.  

A second problem is to insure that the only variable allowed in the context is "n".

Below is a version using MathObjects that works.  It also has some suggestions about the current thinking on best practice for formatting problems so that they are easily maintained and modified.  Of course, as with perl itself, "there is more than one right way to do things" so alternative approaches are possible, but this will help get you started.  In the version below the points at which
the formula is checked are fixed to aid in debugging, but they can be replaced with randomly generated points for a production version.

##DESCRIPTION



##ENDDESCRIPTION


DOCUMENT();        # This should be the first executable line in the problem.

loadMacros(
   "PGstandard.pl",     # Standard macros for PG language
   "MathObjects.pl",
   #"source.pl",         # used to display problem source button
   "contextIntegerFunctions.pl",
   "PGcourse.pl",      # Customization file for the course
);

TEXT(beginproblem());

$showPartialCorrectAnswers = 1;

##############################################################
#
#  Setup
#
#
Context("IntegerFunctions");
Context()->variables->are(n=>'Real'); # get rid of x as a variable -- only variable now is n

$a_n = Formula("n*(n+1)/2");
# reference:  http://webwork.maa.org/wiki/FormulaTestPoints

$a_n->{test_points} = [[4],[6],[8]];  #  this is awkward, but if there were two variables then the points
                                      #  would be [[4,1],[5,2],[7,4]]  etc.
##############################################################
#
#  Text
#
#

Context()->texStrings;  #allows MathObjects to print in TeX
BEGIN_TEXT
What is an explicit expression for the sum
of the first \(n\) positive integers?
$BR


\( a_n = \) \{ ans_rule(40) \}

$PAR
END_TEXT
Context()->normalStrings;


##############################################################
#
#  Answers
#
#

ANS($a_n->cmp);


ENDDOCUMENT();        # This should be the last executable line in the problem.
 
In reply to Michael Gage

Re: Using binomial coefficient notation C(n,r) in answers

by Joy Morris -

Is there a way to do this using a random number for the bottom of the binomial coefficient? I.e. asking the students to input the correct formula for C(n+$k,$k) where $k is a random value? So I assume I'd need to include a loop to get the function correct...?

In reply to Joy Morris

Re: Using binomial coefficient notation C(n,r) in answers

by Glenn Rice -

Here is a variant of Mike's answer that will do what I think you are asking for.  This uses a loop as you said.  I used PGML for this, but you could use PG as Mike did if you must.  I highly recommend switching to PGML though as that is the modern approach.

DOCUMENT();

loadMacros(
    "PGstandard.pl",
    "MathObjects.pl",
    "PGML.pl",
    "contextIntegerFunctions.pl",
    "PGcourse.pl"
);

TEXT(beginproblem());

Context("IntegerFunctions");
Context()->variables->are(n => 'Real');

$k = random(2, 6);
$ans_str = "";
$ans_str .= "(n - $_)" for (1 .. $k);

$ans = Formula($ans_str)->with(test_points => [ map { [2 * $_] } 1 .. $k + 1 ]);

BEGIN_PGML
What is an explicit expression for [`C(n + [$k], [$k])`]?

[`C(n + [$k], [$k]) = `] [_]{$ans}{40}
END_PGML

ENDDOCUMENT();