|
| 1 | +#Fibonacci sequence starts with 0 and 1 as the first numbers and the consequetive numbers are calculated by adding up the two numbers before them |
| 2 | +#0, 1, 1, 2, 3, 5, 8, 13 ... |
| 3 | + |
| 4 | +# Function to claculate list of fibonacci numbers |
| 5 | +def fibonacci_calc(num): |
| 6 | + result = list() |
| 7 | + count = 0 |
| 8 | + # Invalid input |
| 9 | + if num <= 0: |
| 10 | + print("Please enter a positive integer (input>0)") |
| 11 | + return result |
| 12 | + # Input 1 |
| 13 | + elif num == 1: |
| 14 | + result.append(0) |
| 15 | + return result |
| 16 | + else: |
| 17 | + #starting values |
| 18 | + n1, n2 = 0, 1 |
| 19 | + while count < num: |
| 20 | + result.append(n1) |
| 21 | + nth = n1 + n2 |
| 22 | + n1 = n2 |
| 23 | + n2 = nth |
| 24 | + count += 1 |
| 25 | + return result |
| 26 | + |
| 27 | + |
| 28 | +if __name__ == "__main__": |
| 29 | + fibo_list = list() |
| 30 | + n = int(input("Please enter the sequence number: ")) |
| 31 | + fibo_list = fibonacci_calc(n) |
| 32 | + for i in fibo_list: |
| 33 | + print(i) |
0 commit comments