#define MIN(a,b) (a < b ? a : b)
// num = numerator
// den = denominator
// div = current divisor
void reduce(int num, int den, int div)
{
int min = MIN(num, den);
if(div > min)
return; // done
if(!(num%div) && !(den%div)) // can we divide both numerator and denominator?
{
printf("%d/%d\n", num/div, den/div);
reduce(num/div, den/div, div);
}
else
reduce(num, den, ++div); // up the divisor by 1 and try again
}
int main(int argc, char* argv[])
{
reduce(4, 8, 2); // start with a nice low divisor (can't be 1!!)
return 0;
}
i have no idea if this is 100% booletproof, but it sure looked like it at 6am 🙂 if you can find any logic errors drop a line in here. theres absolutly no optimalization being done (like checking for primes, etc), but as far as I know it wroks pretty well. If you have any questions on implementation drop a line over here.