Skip to content

Commit 205698d

Browse files
authored
Update GCD.java
1 parent cc6af5f commit 205698d

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Others/GCD.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
//Oskar Enmalm 3/10/17
22
//This is Euclid's algorithm which is used to find the greatest common denominator
3+
//Overide function name gcd
34

45
public class GCD{
56

6-
public static int gcd(int a, int b) {
7-
8-
int r = a % b;
9-
while (r != 0) {
10-
b = r;
11-
r = b % r;
7+
public static int gcd(int num1, int num2) {
8+
9+
int gcdValue = num1 % num2;
10+
while (gcdValue != 0) {
11+
num2 = gcdValue;
12+
gcdValue = num2 % gcdValue;
1213
}
13-
return b;
14+
return num2;
1415
}
1516
public static int gcd(int[] number) {
1617
int result = number[0];
1718
for(int i = 1; i < number.length; i++)
19+
//call gcd function (input two value)
1820
result = gcd(result, number[i]);
1921

2022
return result;
2123
}
2224

2325
public static void main(String[] args) {
2426
int[] myIntArray = {4,16,32};
27+
//call gcd function (input array)
2528
System.out.println(gcd(myIntArray));
2629
}
2730
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy