-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGreatestCommonDivisor.java
More file actions
78 lines (41 loc) · 897 Bytes
/
GreatestCommonDivisor.java
File metadata and controls
78 lines (41 loc) · 897 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Greatest Common Divisor
Problem Description
Given 2 non negative integers A and B, find gcd(A, B)
GCD of 2 integers A and B is defined as the greatest integer g such that g is a divisor of both A and B. Both A and B fit in a 32 bit signed integer.
Note: DO NOT USE LIBRARY FUNCTIONS.
Problem Constraints
0 <= A, B <= 109
Input Format
First argument is an integer A.
Second argument is an integer B.
Output Format
Return an integer denoting the gcd(A, B).
Example Input
Input 1:
A = 4
B = 6
Input 2:
A = 6
B = 7
Example Output
Output 1:
2
Output 2:
1
Example Explanation
Explanation 1:
2 divides both 4 and 6
Explanation 2:
1 divides both 6 and 7
*/
public class Solution {
public int gcd(int A, int B) {
return GCD(A, B);
}
public int GCD(int A, int B){
if(B==0)
return A;
return GCD(B, A%B);
}
}