Here is another of those algorithms that are of limited use, but I want to keep in my back pocket. In this case, I had to deal with fractions when resizing recipes: a recipe that serves 5 requires 3/8 cups of olive oil – how much oil is needed if we are feeding 72? Just multiplying it out (3/8 / 5) * 72) does not yield a very user-friendly answer ( 216 / 40).

So, to reduce a fraction to it’s simplest terms, use the following:

where
n = numerator
d = denominator
double a;
double b = n;
double c = d;
while c != 0
{
a = b;
b = c;
c = a mod b;
}
n /= b; // or w = floor(n / d); n = mod(n, d) / b;
d /= b;

Feed this n = 3 * 72 / 5 and d = 8 (or 216 and 40) and you will get n = 27, d = 5 (or w=5, n=2, d=5) – *which is far better*.

This method also works for converting decimals to fractions; just use your decimal as the numerator and 1 as the denominator (e.g. n=2.6, d=1 yields n=13, d=5).

### Like this:

Like Loading...

*Related*

## One thought on “Numbers to fractions, or reducing fractions”