0

gcd should be a recursive function. It should return void. It should take two positive integers and place the GCD in the third parameter.

Here is my coded gcd function. However, I realized that it is not a recursive function. How would I change this code so it is a recursive function?

void gcd(int *x, int *y) {
int i;
getValuesForGCD(x, y);
for (i = *x; i >= 1; i--)
{
    if (*x % i == 0 && *y % i == 0)
    {
        printf("The GCD of %d and %d is %d", *x, *y, i); 
        break;
    }
}
}
4
  • 1
    Converting a non-recursive function to a recursive function doesn't sound very practical. Normally people try to eliminate recursion, not introduce it. Why are you trying to introduce recursion? Commented Feb 15, 2013 at 15:09
  • Why the pointers? As a side note, modify the algorithm above such i start from min(x,y). Commented Feb 15, 2013 at 15:11
  • @RaymondChen some things are much more readable and natural in a recursive way, gcd is one of them. Although, in this case it's probably a hw... Commented Feb 15, 2013 at 15:12
  • I don't know what's going on here, but please stop vandalizing your question. I'm locking this for now. Commented Feb 15, 2013 at 22:05

3 Answers 3

6

GCD is naturally defined as a recurrent formulae. It's straightforwardly transformed into a recursive function:

gcd(a, 0) = a
gcd(a, b) = gcd(b, a % b)

Write this in a C format and that's it.

Sign up to request clarification or add additional context in comments.

Comments

2
int gcd(int a, int b) { 
   if (b == 0) 
   then return a
   else return gcd(b, a % b);
}

You should notice that a and b must be non-negative number.

and the nice thing is, this is a tail-recursion, so it run as fast as non-recursive method.

3 Comments

Could run as fast. That will only happen if the compiler does TCO.
@cHao often, if compiler doesn't support TCO, it often doesn't support recursion,too (it means you cannot do recursive method).
BS. Support for recursion requires nothing more than a stack to pass args and return addresses on, and that's typically built into the CPU. Nearly every compiler made in the past 30+ years supports it, and a huge number of those don't do TCO.
1

Normally, people work to remove recursion, not introduce it.

If you want a literal conversion of your iterative algorithm to recursive, it would go like this:

void gcdrecursive(int *x, int *y, int i)
{
    if (i >= 1) {
        if (*x % i == 0 && *y % i == 0) {
            printf("The GCD of %d and %d is %d", *x, *y, i); 
        } else {
            gcdrecursive(x, y, i - 1);
        }
    }
}

void gcd(int *x, int *y) {
    getValuesForGCD(x, y);
    gcdrecursive(x, y, *x);
}

To convert an iterative solution to recursive, you convert each loop to a recursive function that performs one iteration of the loop, then calls itself recursively for the next iteration. Breaking the loop corresponds to stopping the recursion. In your example, the loop breaks for two reasons: Either the GCD is found or i reaches zero.

Note that this is not the best algorithm for gcd, but it takes the function you provided and converts it to recursive, as requested.

1 Comment

@QweRty Don't forget to credit me in your assignment. Otherwise it would be plagiarism.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.