-
Notifications
You must be signed in to change notification settings - Fork 0
/
4주차 실습-(1)
58 lines (53 loc) · 1.07 KB
/
4주차 실습-(1)
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
def gcd(m,n):
if n!= 0:
return gcd(n, m%n)
else:
return abs(m)
def gcd1_1(m,n):
while n != 0:
print("gcd1_1",m,n)
m, n = n, m%n
return abs(m)
def gcd1_2(m,n):
print("gcd1_2")
def loop(m, n, k):
print("gcd1_2, loop",m,n,k)
if not (m == 0 or n == 0):
if m % 2 == 0 and n % 2 == 0:
return loop(m//2,n//2,k+1)
elif m % 2 == 0 and n % 2 == 1:
return loop(m//2,n,k)
elif m % 2 == 1 and n % 2 == 0:
return loop(m,n//2,k)
elif m <= n:
return loop(m,(n-m)//2,k)
else:
return loop(n,(m-n)//2,k)
else:
if m == 0:
return abs(n*k)
else:
return abs(m*k)
return loop(m,n,1)
def gcd1_3(m,n):
print("gcd1_3")
k = 1
while not (m == 0 or n == 0):
print("gcd1_3, while",m,n,k)
if m % 2 == 0 and n % 2 == 0:
m , n , k = m//2, n//2, k+1
elif m % 2 == 0 and n % 2 == 1:
m = m//2
elif m % 2 == 1 and n % 2 == 0:
n = n//2
elif m <= n:
n = (n-m)//2
else:
m , n = n, (m-n)//2
if m == 0:
return abs(n * k)
else:
return abs(m * k)
print(gcd1_1(18,48))
print(gcd1_2(18,48))
print(gcd1_3(18,48))