-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteration vs Recursion.py
More file actions
100 lines (83 loc) · 1.99 KB
/
Iteration vs Recursion.py
File metadata and controls
100 lines (83 loc) · 1.99 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Iteration : Looping construct that updates itself after every iteration of the loop.
Recursion : A function that is calling on itself to solve a problem until a base case is reached.
EXAMPLE : Factorials
"""
def main():
#a = 5
#b = 5
#n = 5
#print(a,"*", b,"=", iter_multiply(a, b))
#print(str(n)+"! =", iter_facorial(n))
#print(str(n)+"! =", recur_factorial(n))
#base = float(input("base ="))
#exp = float(input("exp ="))
#print(iterPower(base, exp))
#a = int(input("a = "))
#b = int(input("b = "))
#print("de grootste gemeenschappelijke deler van", a, "en", b, "is", gcdIter(a, b))
while True:
x = int(input("Give a number : "))
print(fib(x), "is the", x, "number of the Fibonacci sequence.")
def iter_multiply(a, b):
"""
multiply a * b with an Iterative algorithm
"""
result = 0
while b > 0:
result += a
b -= 1
return result
def iter_facorial(n):
"""
Give the factorial of n!
"""
result = 1
for i in range(1, n+1):
result *= i
return result
def recur_factorial(n):
"""
Give the factorial of n!
"""
if n == 0 or n==1:
return 1
else:
return n * recur_factorial(n-1)
def iterPower(base, exp):
'''
base: int or float.
exp: int >= 0
returns: int or float, base^exp
'''
result = 1
while exp > 0:
result *= base
exp -= 1
return result
def gcdIter(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
if a > b :
x = a
y = b
else :
x = b
y = a
for i in range(y):
z = y - i
if x % z == 0 and y % z == 0:
return z
def gcdRecur(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
if b == 0:
return a
else:
return gcdRecur(b, a % b)
if __name__ == '__main__':
main()