Jason
Jason

Reputation: 52527

A way to get a math answer in fraction form

I'm trying to write a program that will help someone study for the GRE math. As many of you may know, fractions are a big part of the test, and calculators aren't allowed. Basically what I want to do is generate four random numbers (say, 1-50) and either +-/* them and then accept an answer in fraction format. The random number thing is easy. The problem is, how can I 1) accept a fractional answer and 2) ensure that the answer is reduced all the way?

I am writing in ASP.NET (or jQuery, if that will suffice). I was pretty much wondering if there's some library or something that handles this kind of thing...

Thanks!

Upvotes: 0

Views: 530

Answers (3)

ilya n.
ilya n.

Reputation: 18826

Learn Python and try fractions module.

Upvotes: 0

kdmurray
kdmurray

Reputation: 3048

Since fractions are essentially divisions you can check to see if the answer is partially correct by performing the division on the fraction entries that you're given.

[pseudocode]
if (answer.contains("/"))
  int a = answer.substring(1,answer.instanceof("/"))
  int b = answer.substring(answer.instanceof("/"))
if (a/b == expectedAnswer)
  if (gcd(a,b) == 1)
    GOOD!
  else
    Not sufficiently reduced
else
  WRONG!

To find out whether it's reduced all the way, create a GCD function which should evaluate to the value of the denominator that the user supplied as an answer.

Upvotes: 0

Related Questions