Skip to content

Commit be803ac

Browse files
committed
gcd-video2.5
1 parent 8bd25e7 commit be803ac

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Diff for: Mathematics/gcd_naive.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ def gcd(m, n):
1919
m = int(input("Enter the first number: "))
2020
n = int(input("Enter the second number: "))
2121
print(f"gcd({m}, {n}) = {gcd(m,n)}")
22+

Diff for: Mathematics/gcd_optimized.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Euclid Algorithm...
2+
# def gcd(m, n):
3+
# if m < n: # Assume m >= n
4+
# (m, n) = (n, m)
5+
6+
# if (m%n) == 0:
7+
# return n
8+
# else:
9+
# diff = m-n
10+
# return gcd(max(n, diff), min(n, diff))
11+
12+
# def gcd(m,n):
13+
# if m < n:
14+
# (m, n)=(n,m)
15+
# while (m%n)!=0:
16+
# diff = m-n
17+
# (m, n) = (max(n, diff), min(n, diff))
18+
19+
# return n
20+
21+
def gcd(m,n):
22+
if m < n:
23+
(m, n) = (n, m)
24+
25+
if m%n == 0:
26+
return n
27+
else:
28+
return gcd(n, m%n)
29+
30+
31+
m = int(input("Enter the first number: "))
32+
n = int(input("Enter the second number: "))
33+
print(f"gcd({m}, {n}) = {gcd(m,n)}")

0 commit comments

Comments
 (0)