forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01. GCD or HCF of two numbers.py
97 lines (70 loc) Β· 2.18 KB
/
01. GCD or HCF of two numbers.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# https://www.geeksforgeeks.org/c-program-find-gcd-hcf-two-numbers/
'''
GCD (Greatest Common Divisor) or HCF (Highest Common Factor)
of two numbers is the largest number that divides both of them.
'''
# Euclidean Algorithm: GCD of two numbers doesnβt change if smaller number is subtracted from a bigger number.
# https://www.geeksforgeeks.org/euclidean-algorithms-basic-and-extended/
# --------------------------------------------------------------------------------------------------
def gcd(a, b):
# Find minimum of a and b
result = min(a, b)
while result:
if a % result == 0 and b % result == 0:
break
result -= 1
# Return the gcd of a and b
return result
# Driver Code
a = 98
b = 56
print(f"GCD of {a} and {b} is {gcd(a, b)}")
# --------------------------------------------------------------------------------------------------
# Recursive function to return gcd of a and b
def gcd(a,b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# base case
if (a == b):
return a
# a is greater
if (a > b):
return gcd(a-b, b)
return gcd(a, b-a)
# Time Complexity: O(max(a,b))
# Auxiliary Space: O(max(a,b))
# ------------------------------------------------------------------------------------------------
# Dynamic Programming Approach (Top Down Using Memoization)
# Taking the matrix as globally
dp = [[-1 for i in range(1001)] for j in range(1001)]
def gcd(a,b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# base case
if (a == b):
return a
if(dp[a][b] != -1):
return dp[a][b]
# a is greater
if (a > b):
dp[a][b] = gcd(a-b, b)
else:
dp[a][b] = gcd(a, b-a)
return dp[a][b]
# Time Complexity: O(max(a,b))
# Auxiliary Space: O(1)
# ------------------------------------------------------------------------------------------------
# Use Modulo Operator in Euclidean Algorithm
def gcd(a,b):
# Everything divides 0
if (b == 0):
return a
return gcd(b, a%b)
# Time Complexity: O(log(max(a,b))
# Auxiliary Space: O(log(max(a,b))