Skip to content

Commit 0bb22fb

Browse files
Remove main function from GCD class (TheAlgorithms#5828)
1 parent ef72b1e commit 0bb22fb

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/main/java/com/thealgorithms/maths/GCD.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* This is Euclid's algorithm, used to find the greatest common
5-
* denominator Override function name gcd
4+
* This class provides methods to compute the Greatest Common Divisor (GCD) of two or more integers.
65
*
6+
* The Greatest Common Divisor (GCD) of two or more integers is the largest positive integer that divides each of the integers without leaving a remainder.
7+
*
8+
* The GCD can be computed using the Euclidean algorithm, which is based on the principle that the GCD of two numbers also divides their difference.
9+
*
10+
* For more information, refer to the
11+
* <a href="https://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest Common Divisor</a> Wikipedia page.
12+
*
13+
* <b>Example usage:</b>
14+
* <pre>
15+
* int result1 = GCD.gcd(48, 18);
16+
* System.out.println("GCD of 48 and 18: " + result1); // Output: 6
17+
*
18+
* int result2 = GCD.gcd(48, 18, 30);
19+
* System.out.println("GCD of 48, 18, and 30: " + result2); // Output: 6
20+
* </pre>
721
* @author Oskar Enmalm 3/10/17
822
*/
923
public final class GCD {
@@ -40,20 +54,12 @@ public static int gcd(int num1, int num2) {
4054
* @param numbers the input array
4155
* @return gcd of all of the numbers in the input array
4256
*/
43-
public static int gcd(int[] numbers) {
57+
public static int gcd(int... numbers) {
4458
int result = 0;
4559
for (final var number : numbers) {
4660
result = gcd(result, number);
4761
}
4862

4963
return result;
5064
}
51-
52-
public static void main(String[] args) {
53-
int[] myIntArray = {4, 16, 32};
54-
55-
// call gcd function (input array)
56-
System.out.println(gcd(myIntArray)); // => 4
57-
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d%n", gcd(40, 24), gcd(24, 40)); // => 8
58-
}
5965
}

src/test/java/com/thealgorithms/maths/GCDTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ void test7() {
4040
Assertions.assertEquals(GCD.gcd(9, 6), 3);
4141
}
4242

43+
@Test
44+
void test8() {
45+
Assertions.assertEquals(GCD.gcd(48, 18, 30, 12), 6);
46+
}
47+
4348
@Test
4449
void testArrayGcd1() {
4550
Assertions.assertEquals(GCD.gcd(new int[] {9, 6}), 3);

0 commit comments

Comments
 (0)