JavaAlgorithms/Maths/GCD.java

58 lines
1.4 KiB
Java
Raw Normal View History

2019-10-08 12:37:17 +08:00
package Maths;
/**
* This is Euclid's algorithm which is used to find the greatest common denominator
* Overide function name gcd
*
* @author Oskar Enmalm 3/10/17
*/
2018-07-25 23:07:34 +08:00
public class GCD {
2019-10-08 12:37:17 +08:00
/**
* get greatest common divisor
*
* @param num1 the first number
* @param num2 the second number
* @return gcd
*/
2018-07-25 23:07:34 +08:00
public static int gcd(int num1, int num2) {
2019-10-08 12:37:17 +08:00
if (num1 < 0 || num2 < 0) {
throw new ArithmeticException();
}
2018-07-25 23:07:34 +08:00
2019-10-08 12:37:17 +08:00
if (num1 == 0 || num2 == 0) {
return Math.abs(num1 - num2);
2017-10-03 16:02:42 +08:00
}
2018-07-25 23:07:34 +08:00
2019-10-08 12:37:17 +08:00
while (num1 % num2 != 0) {
int remainder = num1 % num2;
num1 = num2;
num2 = remainder;
}
return num2;
2017-10-03 16:02:42 +08:00
}
2018-07-25 23:07:34 +08:00
2019-10-08 12:37:17 +08:00
/**
* get greatest common divisor in array
*
* @param number contains number
* @return gcd
*/
2018-07-25 23:07:34 +08:00
public static int gcd(int[] number) {
int result = number[0];
for (int i = 1; i < number.length; i++)
// call gcd function (input two value)
result = gcd(result, number[i]);
return result;
}
public static void main(String[] args) {
int[] myIntArray = {4, 16, 32};
2018-07-25 23:07:34 +08:00
// call gcd function (input array)
System.out.println(gcd(myIntArray)); // => 4
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d\n", gcd(40, 24), gcd(24, 40)); // => 8
2017-10-28 03:43:04 +08:00
}
2017-10-28 02:19:55 +08:00
}