TDT4109/Exercise 3/8.py

19 lines
515 B
Python

def fibonacciIterative(n):
k1 = 0
k2 = 1
for i in range(n - 1):
previousK2 = k2
k2 = k1 + k2
k1 = previousK2
return k1
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)]
if __name__ == "__main__":
n = int(input('n: '))
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))