## Exercise 1.1 ```python def zorglub(n: int) -> int: j = 1 k = 0 i = 1 while i <= n: j = i * j k = j + k i = i + 1 return k ``` The function return the sum of the factorial of n ## Exercise 1.2 ```python def multiplication(x: int, y: int) -> int: result = 0 while y > 0: m += x y -= 1 return result def multiplication2(x: int, y: int) -> int: # Thanks Hugo :p if y < 0: y = -y if x < 0: return multiplication(-x, y) return -multiplication(x, y) else return multiplication(x, y) def productZ(x, y): pos = True if x < 0: x = -x pos = False if y < 0: y = -y pos = not pos m = multiplication(x, y) if not pos: m = -m return m ``` ## Exercise 1.3 ```python def exponetial(x: int, n: int) -> int: result = 0 while n > 0: m *= x n -= 1 return result def power(x, n): if x == 0; if n == 0: raise Exception("0 power 0 undefinded") else: return 0 elif x == 1: return 1 elif x == -1: return (n%2)*(-2) + 1 else: res = 1 while n > 0: res *= x n -= 1 return res ``` ## Exercise 1.4 ```python def fibo(n): prev = 1 #f0 cur = 1 #f1 while n> 1: (prev, cur) = (cur, prev+cur) n -= 1 return cur # OR def fibo(n): prev = 1 cur = 1 i = 1 while i < n: prev += cur cur += prev i += 2 if i == n: # i % 2 == 1 return cur else: return prev ``` ## Exercise 1.5 1) ```python def my_sum(n: int) -> int: s = 0 i = 1 while i <= n: s += u(i) i += 1 return s # OR def my_sum(n: int) -> int: s = 0 while n > 0: s += u(n) n -= 1 return s ``` 2) ```python def my_sum(n: int) -> int: ss = 0 i = 1 while i <= n: si = 0 j = 1 while j <= i: si += u(j) j += 1 ss += si i += 1 return ss # OR def my_sum(n: int) -> int: ss = 0 i = 1 while i <= n: j = 1 while j <= o: ss += u(j) j += 1 i += 1 return ss # OR def mu_sum(n: int) -> int: ss = 0 si = 0 i = 1 while i <= n: si += u(i) ss += si i += 1 return ss ``` ## Exercise 2.1 ```python def euclid(a, b): if (a <= b): raise Exception("a must be superior to b") r = 1 while r != 0: oldr = r q = a//b r = a%b a = b b = r return oldr # OR def euclid(a, b): while b != 0: (a, b) = (b, a%b) return a ``` ## Exercise 2.2 ```python def mirror(n: int) -> int; res = 0 while n != 0: res = res * 10 + n % 10 n //= 10 return res ``` ## Exercise 2.3 ```python def factorial(limit: n) -> int: n = 0 if limit <= n: return n else: f = 1 while f < limit: n += 1 f *= n if n % 2 == 0: return (n-2) else: return (n-1) ``` ## Exercise 2.4 ```python def prime(n: int) -> int: """ Check if n(int) is prime if n > 1, error otherwise """ if n <= 1: raise Exception("prime: n<= 1") elif n % 2 == 0: return n == 2 else: d = 3 while d*d <= n and n % d!= 0: d += 2 return d*d > n ``` ## Exercise 3.3 ```python def quotient(a: int, b: int) -> int: q = 0 if (a < b): (a, b) = (b, a) while (a-b) >= 0: q += 1 a -= b return q