TDT4109/Exercise 3/8.py

19 lines
515 B
Python
Raw Normal View History

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__":
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))