Update GCD.java
This commit is contained in:
parent
cc6af5f59e
commit
205698d90f
@ -1,20 +1,22 @@
|
|||||||
//Oskar Enmalm 3/10/17
|
//Oskar Enmalm 3/10/17
|
||||||
//This is Euclid's algorithm which is used to find the greatest common denominator
|
//This is Euclid's algorithm which is used to find the greatest common denominator
|
||||||
|
//Overide function name gcd
|
||||||
|
|
||||||
public class GCD{
|
public class GCD{
|
||||||
|
|
||||||
public static int gcd(int a, int b) {
|
public static int gcd(int num1, int num2) {
|
||||||
|
|
||||||
int r = a % b;
|
int gcdValue = num1 % num2;
|
||||||
while (r != 0) {
|
while (gcdValue != 0) {
|
||||||
b = r;
|
num2 = gcdValue;
|
||||||
r = b % r;
|
gcdValue = num2 % gcdValue;
|
||||||
}
|
}
|
||||||
return b;
|
return num2;
|
||||||
}
|
}
|
||||||
public static int gcd(int[] number) {
|
public static int gcd(int[] number) {
|
||||||
int result = number[0];
|
int result = number[0];
|
||||||
for(int i = 1; i < number.length; i++)
|
for(int i = 1; i < number.length; i++)
|
||||||
|
//call gcd function (input two value)
|
||||||
result = gcd(result, number[i]);
|
result = gcd(result, number[i]);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -22,6 +24,7 @@ public class GCD{
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int[] myIntArray = {4,16,32};
|
int[] myIntArray = {4,16,32};
|
||||||
|
//call gcd function (input array)
|
||||||
System.out.println(gcd(myIntArray));
|
System.out.println(gcd(myIntArray));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user