views:

221

answers:

3

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!

A: 

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.

kdmurray
how could i write a (L)CD function other than looping through every number in my range and doing a ridiculous amount of calculation?
Jason
Use Euclid's Algorithm: http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
Amber
This code snippet contains several errors:(1) A fraction is reduced if gcd(a,b) == 1 not gcd(a,b) == b.(2) Comparisons with floating points must allow some slack for rounding errors: if (a/b == expectedAnswer) might reject some correct (but not reduced) answers due floating point rounding errors.
Accipitridae
1. point taken, I was half asleep (corrected)2. true, hence "pseudocode"
kdmurray
A: 

Learn Python and try fractions module.

ilya n.