-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecur_iter.py
72 lines (56 loc) · 1.27 KB
/
recur_iter.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
def iterPower(base, exp):
'''
base: int or float.
exp: int >= 0
returns: int or float, base^exp
'''
result = 1
for _ in range(exp):
result *= base
return result
# base should mutliple itself by exp times
def recurPower(base, exp):
'''
base: int or float.
exp: int >= 0
returns: int or float, base^exp
'''
if exp == 0:
return 1
else:
result = base*recurPower(base,exp-1)
return result
def fourthPower(x):
'''
x: int or float.
'''
# Your code here
return square(x) * square(x)
def odd(x):
'''
x: int or float.
returns: True if x is odd, False otherwise
'''
return x%2!=0
def gcdRecur(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
# Your code here
if b == 0:
return a
else:
return gcdRecur(b, a % b)
def gcdIter(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
# Your code here
testValue = min(a,b)
while testValue > 0:
if a%testValue == 0 and b%testValue == 0:
return testValue
else:
testValue -= 1