forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCD.java
35 lines (28 loc) · 807 Bytes
/
GCD.java
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
34
35
/*GCD of two number is the largest number by which the given number is divisable
i.e.
if the given two numbers are A(>0) and B(>0) and their GCD is G
then A%G==0 and B%G==0 and G is the Biggest number*/
import java.util.Scanner;
public class EuclideanGCD {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int firstNumber=sc.nextInt();
int secondNumber=sc.nextInt();
System.out.println(GCD(firstNumber,secondNumber));
}
public static int GCD(int A,int B){
if(B==0){
return(A);
}
return(GCD(B,(B%A)));
}
}
/*
Test Cases:
Input: 12 16
Output:4
Input: 4 1
Output: 1
Time Complexity: O(log(max(a,b)))
Space Complexity: 1
*/