Is it possible to reduce a fraction to the lowest form, in a single recursive pass, using no auxiliary function other than is_zero, succ and pred? As an example, if we wanted to implement gcd that way, we could write it as follows:
// gcd(a, b, c) returns the greatest common divisor of `a+c` and `b+c`
function gcd(a, b, c)
if is_zero(a):
if is_zero(b):
c
else:
gcd(c, b, 0)
else:
if is_zero(b):
gcd(a, c, 0)
else:
gcd(pred(a), pred(b), succ(c))
Notice how this function is a direct recursion that does not use any auxiliary function other than is_zero, succ and pred. It does require one extra argument, though, which stores the result. It is also tail-recursive, but that isn't a demand. My question is: is it possible to do the same, but for reduce_fraction?
// reduce_fraction(a, b, ...) returns a pair
// with the `a/b` fraction in the lowest form
function reduce_fraction(a, b, ...):
...
Note that using GCD to implement reduce_fraction isn't allowed:
function reduce_fraction(a,b):
return (a / gcd(a,b), b / gcd(a,b))
Because reduce_fraction would call 2 auxiliary functions (gcd and /), which is not allowed by definition. It must be a direct recursion using only is_zero, succ and pred.
I'm specifically looking for a solution that reduces the space used (number of auxiliary arguments) and time used (total recursive steps).