Skip to content

Commit 1a37db6

Browse files
committed
GCF
1 parent 712be13 commit 1a37db6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/easy/Gcf.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package easy;
2+
3+
/**
4+
* Have the function GCF(arr) take the array of numbers stored in arr
5+
* which will always contain only two positive integers,
6+
* and return the greatest common factor of them.
7+
* ---
8+
* For example: if arr is [45, 12] then your program should return 3.
9+
* There will always be two elements in the array,
10+
* and they will be positive integers.
11+
*/
12+
public class Gcf {
13+
14+
/**
15+
* Greatest Common Factor function.
16+
*
17+
* @param arr input array of integers
18+
* @return the greatest common factor
19+
*/
20+
private static int gcf(int[] arr) {
21+
if (arr[0] == 0 || arr[1] == 0) {
22+
return arr[0] + arr[1];
23+
}
24+
return gcf(new int[]{arr[1], arr[0] % arr[1]});
25+
}
26+
27+
/**
28+
* Entry point.
29+
*
30+
* @param args command line arguments
31+
*/
32+
public static void main(String[] args) {
33+
var result1 = gcf(new int[]{64, 128, 256, 512});
34+
System.out.println(result1);
35+
var result2 = gcf(new int[]{12, 28});
36+
System.out.println(result2);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)