Popular Posts

Saturday, June 25, 2011

GCD

public static int getGcd(int a,int b){
int temp;
while(b!=0){
temp = a%b;
a=b;
b=temp;
}
return a;
}
Recursive approach :

public static int getGCDRecursively(int a,int b){
if(b==0) return a;
else if(b>a) return getGCDRecursively(b,a);
else return getGCDRecursively(b, a%b);
}

Co-prime numbers : Two numbers are called relatively prime, or coprime if their greatest common divisor equals 1. For example, 9 and 28 are relatively prime.

No comments:

Post a Comment