public int gcd(int a, int b) {
for(int div = minimum(a, b); div >= 1; --div) {
if(a % div == 0 && b % div == 0) {
return div;
}
}
}
Let's consider two other numbers 540 and 600 and find their gcd.
540 | 600 | 1
540
-----------------
60 | 540 | 9
540
------------------
X
That is, gcd(600, 540) = gcd(540, 600 mod 540)
= gcd(540, 540 mod 60)
= gcd(60, 0)
= 60
public int gcdEucledian(int a, int b) {
while(b > 0) {
int temp = a % b;
a = b;
b = temp;
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)