forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuclideanGCD.java
More file actions
33 lines (28 loc) · 777 Bytes
/
Copy pathEuclideanGCD.java
File metadata and controls
33 lines (28 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class EuclideanGCD {
/**
* Calculates the greatest common divisor of two natural numbers using the Euclidean algorithm.
*
* @param a natural number
* @param b natural number
* @return the largest natural number that divides a and b without leaving a remainder
*/
public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
System.out.println(gcd(10, 5));
System.out.println(gcd(5, 10));
System.out.println(gcd(10, 8));
System.out.println(gcd(8, 2));
System.out.println(gcd(7000, 2000));
System.out.println(gcd(2000, 7000));
System.out.println(gcd(10, 11));
System.out.println(gcd(11, 7));
System.out.println(gcd(239, 293));
}
}