-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGCD.py
More file actions
30 lines (22 loc) · 654 Bytes
/
GCD.py
File metadata and controls
30 lines (22 loc) · 654 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
# a set of programs to calculate the GCD of 2 numbers.
# Two alternate approches have been identitied: one is iterative and the other is recursive
def gcdIter(a,b):
if a>=b:
for i in range(b,0,-1):
if(a%i==0 and b%i==0):
return i
return 1
else:
for i in range(a,0,-1):
if(b%i==0 and a%i==0):
return i
return 1
gcdIter(323,306) #well done, this wasn't a cakewalk
def gcdRecur(a,b):
if b%a==0:
return a
if a>=b:
return gcdRecur(b,a%b)
if b>a:
return gcdRecur(b,a)
gcdRecur(10,5)