Skip to content

Commit c8038cd

Browse files
authored
Merge pull request TheAlgorithms#283 from teerapat1739/patch-5
Increase recursive GCD
2 parents d7fcdf4 + 205698d commit c8038cd

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Others/GCD.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +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;
15+
}
16+
public static int gcd(int[] number) {
17+
int result = number[0];
18+
for(int i = 1; i < number.length; i++)
19+
//call gcd function (input two value)
20+
result = gcd(result, number[i]);
21+
22+
return result;
23+
}
24+
25+
public static void main(String[] args) {
26+
int[] myIntArray = {4,16,32};
27+
//call gcd function (input array)
28+
System.out.println(gcd(myIntArray));
1429
}
1530
}

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