WeBWorK Problems

Checking 1-(-1)^n

Checking 1-(-1)^n

by Ian Delbridge -
Number of replies: 3

I was wondering if anyone could help me with a specific problem that I evidently don't know how to fix. 

I'm trying to check the answer "1-(-1)^n". I want n as a variable rather than a named constant as to check when n is an even integer as well as an odd integer. When I don't restrict the limits that n is tested at, I always get an error "can't generate enough valid points for comparison". So, I want restrict the domain to integers to keep it from trying to test roots of a negative number, giving imaginary values.

Is there a setting that I can choose that will only check n at integers?

I tried giving the following command:
However, instead of fixing the problem, it causes a new one it would seem, because it doesn't accept the correct answer now, despite getting rid of the "can't generate enough valid points for comparison" error. I might be using wrong syntax here, but I'm not sure.

Any thoughts would be appreciated.
Problem file (as a .txt file) attached.
In reply to Ian Delbridge

Re: Checking 1-(-1)^n

by Paul Pearson -
Hi Ian,

There was a post to the forum about this a while back.  One way to achieve the desired effect is to set the resolution:

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

The documentation for other ways to accomplish this can probably be found at:
  • http://webwork.maa.org/wiki/Answer_Checkers_and_the_Context
  • http://webwork.maa.org/wiki/FormulaTestPoints
Best regards,

Paul Pearson
In reply to Paul Pearson

Re: Checking 1-(-1)^n

by Ian Delbridge -
Awesome! That works completely and seamlessly - exactly the kind of function I was hoping for. Thank you!
In reply to Paul Pearson

Re: Checking 1-(-1)^n

by Danny Glin -
This leaves open the possibility that the (randomly chosen) test points will either all be even or all be odd.  You may want to add:
$f->{test_at} = [[2],[3]];
(where $f is your function)

to ensure that you are testing at at least one even and one odd value.