0

I'm trying to write a simple program which asks for 5 numbers and outputs their GCD. I've already discovered how to do this with two numbers with a simple method:

private static int gcd(int number1, int number2) //Finds GCD of 2 numbers.
{
    if(number2 == 0)
    {
        return number1;
    }
    return gcd(number2, number1%number2);
}

The actual math in the return statement is what confuses me though and I'm not sure how I would write that out with 5 or even more numbers. I've heard that doing this method recursively such as with "gcd(a,b,c)=gcd(gcd(a,b),c)" is the best method, but I guess I'm having trouble with the actual logic of the math in question. I just need a good starting point, really, with how to return 3 numbers, then 4, then 5, etc. I think once I get the logic portion down I'll understand how to do this much easier.

Hydlide
  • 361
  • 2
  • 5
  • 18

2 Answers2

3

You should treat your existing gcd(int, int) method as a "black box"; your new gcd(int, int, int, int, int) method can call it without knowing how it works. You would write:

private static int gcd(int a, int b, int c, int d, int e)
{
    return gcd(gcd(a, b), gcd(gcd(c, d), e));
}

Or, for a somewhat more general solution, you could write a method gcd(int, int...) that takes any positive number of arguments, by using Java 5's var-args support:

private static int gcd(int number1, int... otherNumbers)
{
    int result = number1;
    for(int number: otherNumbers)
        result = gcd(result, number);
    return result;
}

(Note that in both cases, this function is not "recursive" in the programming sense. The former approach does recursively nest its invocations of gcd(int, int), but this is not quite what programmers mean by "recursion". Your original gcd(int, int) function, however, is recursive, because it actually calls itself.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Ah okay, yes I had something similar but it was giving me errors so I was assuming my issue was just sorting the ints within the return statement. I'll play a bit with it now. Thanks. :) – Hydlide Mar 12 '13 at 00:43
3

Here's a material perspective on greatest common factors. Think of a rectangular piece of floor with the dimensions being m and n. The GCD of m and n is the dimension of the largest square tile that will fit perfectly onto that floor.

So in the algorithm you're using, you start with two numbers such as 8 and 10. Your program then repeats the process with one number (say 8) and the modulus of the two numbers, which is 2. This is equivalent to chopping off an 8x8 section because we know that whatever tiles go into the leftover bit will also fit into that area. We're left with a 2x8 section. Repeating the process, we'll get 2 as the GCD. I hope that clears up the actual meaning of the algorithm you're using.

So extending this concept to the GCD of three numbers, we can say that the GCD of m, n, and p is the largest cubic block that will fit into a rectangular prism with dimensions m x n x p. To find this GCD, we first find the square tile that will fit into one of the faces of the prism. Then we can use this dimension to cut a cross-section, so to speak, of the prism and take the GCD of that cross-section. Of course, this can be extended into higher dimensions that we can't exactly visualize!

architectpianist
  • 2,562
  • 1
  • 19
  • 27