WeBWorK Problems

Factorials as a Formula object

Factorials as a Formula object

by Bill Shillito -
Number of replies: 1

Hello,

I authored the following problem, but WeBWorK always gives the "cannot generate enough valid points for comparison" on the last formula (n!). I've tried giving it specific integer points to test but then it tells me it can't test those points. What do I do?

 

DOCUMENT();

loadMacros(
   "PGstandard.pl",     # Standard macros for PG language
   "MathObjects.pl",
   "contextFraction.pl",
   "PGML.pl",
   "scaffold.pl",
   
   "PGchoicemacros.pl",
   "PGstandard.pl",
"PGunion.pl",
"PGnumericalmacros.pl",
"PGstatisticsmacros.pl",
"MathObjects.pl",
"parserPopUp.pl",
"PGML.pl",
"unionTables.pl",
"niceTables.pl",
"PGcourse.pl",
"PGchoicemacros.pl",
"answerHints.pl",
"weightedGrader.pl",
"parserRadioButtons.pl",
"parserNumberWithUnits.pl",
"randomizers.pl"
);


###########################
#  Setup

Context("Numeric");
Context()->variables->add(n => 'Real');
Context()->{format}{number} = "%.9f#";

$a = random(2,5);
$b = random(2,5);

$fact = Formula("n!");
$log = Formula("ln(n)");
$pow = Formula("n^($a)");
$powlog = Formula("n^($a)*ln(n)");
$exp = Formula("$b^n");
$powexp = Formula("n^($a)*$b^n");

$ans1 = $log;
$ans2 = $pow;
$ans3 = $powlog;
$ans4 = $exp;
$ans5 = $powexp;
$ans6 = $fact;

$seed = random(1,5);
if ($seed == 1) {
    $func1 = $fact;
    $func2 = $log;
    $func3 = $pow;
    $func4 = $powlog;
    $func5 = $exp;
    $func6 = $powexp;
} elsif ($seed == 2) {
    $func1 = $powexp;
    $func2 = $fact;
    $func3 = $powlog;
    $func4 = $exp;
    $func5 = $log;
    $func6 = $pow;
} elsif ($seed == 3) {
    $func1 = $log;
    $func2 = $exp;
    $func3 = $powexp;
    $func4 = $powlog;
    $func5 = $fact;
    $func6 = $pow;
} elsif ($seed == 4) {
    $func1 = $pow;
    $func2 = $log;
    $func3 = $fact;
    $func4 = $powlog;
    $func5 = $exp;
    $func6 = $powexp;
} elsif ($seed == 5) {
    $func1 = $powlog;
    $func2 = $fact;
    $func3 = $pow;
    $func4 = $exp;
    $func5 = $powexp;
    $func6 = $log;
}

###########################
#  Main text

BEGIN_PGML

A big part of having good intuition for the Ratio Test is knowing which functions grow faster than which other functions.

Recall that we say [`a_n`] is *dominated* by [`b_n`] (written [`a_n\prec b_n`]) if 
[``\lim\limits_{n\to\infty} \dfrac{a_n}{b_n}=0``] (or, equivalently, if 
[``\lim\limits_{n\to\infty} \dfrac{b_n}{a_n}=\infty``]). Essentially it means that [`a_n`] grows more slowly than [`b_n`].

Rank the following six functions in order from *slowest-growing* to *fastest-growing*:
    
[```[$func1],\quad [$func2],\quad [$func3],\quad [$func4],\quad [$func5],\quad [$func6]```]

[_]{$ans1} (slowest)

[`\prec`] [_]{$ans2}

[`\prec`] [_]{$ans3}

[`\prec`] [_]{$ans4}

[`\prec`] [_]{$ans5}

[`\prec`] [_]{$ans6}  (fastest)

END_PGML

############################
#  Answer evaluation


############################
#  Solution

BEGIN_PGML_SOLUTION

The slowest-growing is [`[$ans1]`].

Next comes [`[$ans2]`], since powers grow faster than logarithms.

Next comes [`[$ans3]`], since multiplying by an extra logarithm makes the function grow faster.

Next comes [`[$ans4]`], since exponentials grow faster than powers.

Next comes [`[$ans5]`], since multiplying by an extra power makes the function grow faster.

Finally, the fastest-growing is [`[$ans6]`].

Hence we have:

[```[$ans1]\prec[$ans2]\prec[$ans3]\prec[$ans4]\prec[$ans5]\prec [$ans6]```]

END_PGML_SOLUTION

COMMENT('MathObject version. Uses PGML.');

ENDDOCUMENT();
In reply to Bill Shillito

Re: Factorials as a Formula object

by Danny Glin -

This question looks like a perfect candidate for DraggableProofs.  When students are asked to enter answers chosen from a fixed list I think it's bad practice to force them to type free-form text.

Since factorial is only defined for nonnegative integers you do need to specify to only evaluate the function at integer values.  I suspect that the reason it wasn't working for you is because you've added the variable n to the context, which leaves any existing variables in the context (i.e. x).  Since there are two variables, if you use 'test_at' you will need to specify pairs of numbers as test points (i.e. values for both n and x).

Here is what you probably want:

Context()->variables->are(n => ['Real', limits=>[1,10], resolution=>1]);

Using "are" instead of "add" removes all other variables from the context.  The remaining arguments specify that the values that n can take on are between 1 and 10, going up by 1, so you will only get integer values.