Quote (mikey00004 @ Mar 5 2014 12:01am)
I'm a new coder looking to get better and find people of like mind. I'm currently developing a game in java to kill some free time and help learn better the language. Here is a copy of the most recent assignment i had to turn in for class, let me know what you think. It grabs 2 numbers from the user and finds the greatest common divisor of both.
If you are looking at improvements to the algorithm? I would say Euclid did it best:
Code
public static int euclid(int A, int B)
{
int a = Math.max(A,B);
int b = Math.min(A,B);
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
Just to highlight the performance boost. Running your algorithm vs "mine" with this input
Code
gcd(1073741824,2)
Yields
Code
Mine: iterations (1) 2
Yours: iterations (536870912) 2
Pretty significant, don't you think?
Code
gcd(1073741824,1231236)
yields
Code
Mine: iterations (8) 4
Yours: iterations (1001) 4
This post was edited by Minkomonster on Mar 4 2014 11:55pm