您的位置:首页 > 运维架构

Euclid's GCD Algorithm——MIT Open Courseware(Computability)

2015-06-27 21:10 477 查看

Euclid’s GCD algorithm

An example of ancient computational thinking, a really wonderful non-obvious efficient algorithm is Euclid’s GCD algorithm. It starts with this question.

How do you reduce a fraction like 510/646 to lowest terms?


Well, we know we need to take out the greatest common divisor (GCD). How do you do that? The grade school method is to factor both numbers; the product of all the common prime factors is the GCD, and we simply cancel them out. But we said before that factoring is believed to be hard. The brute force method is OK for grade school problems, but it won’t do for thousand-digit numbers. But just like testing whether a number is prime or composite, it turns out that the GCD problem can be solved in a different, more clever way—one that doesn’t require factoring.

Euclid’s clever observation was that if a number divides two numbers, say 510 and 646, it also divides any integer linear combination of them, say 646 − 510. [Do you see why this is so?] In general, when we divide B by A, we get a quotient q and a remainder r connected by the equation B = qA + r, which means that r = B − qA, which means that r is a linear combination of A and B!

So finding the GCD of 510 and 646 is the same as finding the GCD of 510 and the remainder when we divide 646 and 510. This is great because the remainder is a smaller number. We’ve made progress!

GCD(510, 646) = GCD(136, 510)


And we can keep doing the same thing.

GCD(136, 510) = GCD(102, 136) = GCD(34, 102) = 34


Here we stopped because 34 divides 102, and we know this means that 34 is the GCD of 34 and 102. We could also take it a step further and appeal to the fact that the GCD of any number and 0 is that number: GCD(34, 102) = GCD(0, 34) = 34.

Pseudo-code:

GIVEN: natural numbers A, B

Assume B is the larger (otherwise swap them)

If A is 0 return B

Else find the GCD of (B % A) and A

Above is from MIT Open Courseware materials. Something about
computability
.


Java implement

/**
* @Author: heartsuit
* @DateTime: 2015-06-27 19:35:21
* @Function: Euclid’s GCD Algorithm
* How do you reduce a fraction like 510/646 to lowest terms?* @Instruction: GCD: The greatest common divisor
* Note: the input arguments must be natural numbers, i.e. the negtive numbers are invalid!
*/
public class GCD {
public int getGCD_Euclid(int a, int b) {
// Assume b is the larger one
if (a > b) {
int temp = a;
a = b;
b =
b998
temp;
}

// Recursion
if (a == 0) {
return b;
} else {
return getGCD_Euclid(a, b%a);
}
}

public int getGCD_WithoutSwap(int a, int b) {
// Recursion
if (a == 0) {
return b;
} else {
return getGCD_WithoutSwap(b%a, a); //Note the order of input arguments
}
}

public int getGCD_WithoutRecursion(int a, int b) {
while(a != 0) {
int remainder = b % a;
b = a;
a = remainder;
}
return b;
}

public static void main(String[] args) {
GCD gcd = new GCD();

// Test cases
int[] a = new int[]{0, 1, 6, 0, 510, 646};
int[] b = new int[]{0, 1, 0, 6, 646, 510};

for (int i = 0; i < a.length; i++) {
System.out.print(gcd.getGCD_Euclid(a[i], b[i]) + " ");
System.out.print(gcd.getGCD_WithoutSwap(a[i], b[i]) + " ");
System.out.print(gcd.getGCD_WithoutRecursion(a[i], b[i]));
System.out.println();
}
}
}


If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: