JavaAlgorithms/Others/GCD.java

25 lines
547 B
Java
Raw Normal View History

2017-10-03 16:02:42 +08:00
//Oskar Enmalm 3/10/17
//This is Euclid's algorithm which is used to find the greatest common denominator
public class GCD{
public static int gcd(int a, int b) {
int r = a % b;
while (r != 0) {
b = r;
r = b % r;
}
return b;
}
}
2017-10-28 02:19:55 +08:00
//Increase the number of calculations.
//Use functoin from above as recursive.
public static int gcd(int[] number) {
int result = number[0];
for(int i = 1; i < number.length; i++)
result = gcd(result, number[i]);
return result;
}