Skip to content

Instantly share code, notes, and snippets.

@codelance
Created December 2, 2012 01:09
Show Gist options
  • Save codelance/4186304 to your computer and use it in GitHub Desktop.
Save codelance/4186304 to your computer and use it in GitHub Desktop.
GCD
private static int gcd(int x, int y){
if( y <= x & (x%y) == 0)
return y;
else if( x < y)
return gcd(y,x);
else
return gcd(y, x % y);
}
private static int iterativegcd(int x, int y)
{
int end = 0;
int winner = 0;
if( x < y)
end = y;
else
end = x;
winner = 1;
for(int i = 1; i <= end; i++)
{
if( (x%i) == 0 & (y%i) == 0 )
winner = i;
}
return winner;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment