FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fixed bug in method gcd(int, int) by christianbender · Pull Request #472 · TheAlgorithms/Java · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
52 changes: 30 additions & 22 deletions Others/GCD.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@
//This is Euclid's algorithm which is used to find the greatest common denominator
//Overide function name gcd

public class GCD{

public static int gcd(int num1, int num2) {

int gcdValue = num1 % num2;
while (gcdValue != 0) {
num2 = gcdValue;
gcdValue = num2 % gcdValue;
public class GCD {

public static int gcd(int num1, int num2) {

if (num1 == 0)
return num2;

while (num2 != 0) {
if (num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
return num2;

return num1;
}
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};
//call gcd function (input array)
System.out.println(gcd(myIntArray));

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 };

// 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
}
}

Back | FazBrowse Home | New Git URL