TDT4109/Exercise 3/task8.py

25 lines
687 B
Python
Raw Normal View History

2020-09-23 11:09:25 +02:00
try:
from common.inputChecking.typeCheck import inputTypeCheck
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
2020-09-14 14:34:03 +02:00
def fibonacciIterative(n):
k1 = 0
k2 = 1
2020-09-14 16:36:09 +02:00
for i in range(n - 1):
2020-09-14 14:34:03 +02:00
previousK2 = k2
k2 = k1 + k2
k1 = previousK2
return k1
2020-09-14 16:36:09 +02:00
fibonacciSum = lambda n: sum([fibonacciIterative(i) for i in range(1, n + 1)])
fibonacciList = lambda n: [fibonacciIterative(i) for i in range(1, n + 1)]
2020-09-14 14:34:03 +02:00
if __name__ == "__main__":
2020-09-23 11:09:25 +02:00
n = inputTypeCheck('n: ', int)
2020-09-14 14:34:03 +02:00
print(f'a) Fibonacci({n})', fibonacciIterative(n))
print(f'b) Sum av Fibonacci(1..{n})', fibonacciSum(n))
print(f'c) Sum av Fibonacci(1..{n})', fibonacciList(n))