-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreatest_common_divisor.py
More file actions
46 lines (39 loc) · 884 Bytes
/
greatest_common_divisor.py
File metadata and controls
46 lines (39 loc) · 884 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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 13 22:52:27 2017
@author: Марианна
"""
def gcdIter(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
# Your code here
if a <= b:
i = a
while a % i != 0 or b % i != 0:
i -= 1
else:
i = b
while a % i != 0 or b % i != 0:
i -= 1
return i
def gcdRecur(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
# Your code here
if a < b:
a, b = b, a
if b == 0:
return a
elif a > b:
return gcdRecur(b, a%b)
print(gcdIter(216, 198))
print(gcdIter(60, 54))
print(gcdIter(27, 54))
print("----------")
print(gcdRecur(156, 216))
print(gcdRecur(40, 56))
print(gcdRecur(27, 30))