Fibonacci
The Fibonacci sequence is a famous mathematical pattern credited to Italian mathematician Fibonacci in the 13th century (though others had discovered it even earlier). The sequence begins with 0 and 1, and the next number is always the sum of the previous two numbers. The sequence continues forever:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987 …
The Fibonacci sequence has applications in music composition, stock market prediction, the pattern of florets in the head of sunflowers, and many other areas. This program lets you calculate the sequence as high as you are willing to go. More information about the Fibonacci sequence can be found at https://en.wikipedia.org/wiki/Fibonacci_number.
fibonacci.py
1"""Fibonacci Sequence, by Al Sweigart al@inventwithpython.com
2Calculates numbers of the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13...
3This code is available at https://nostarch.com/big-book-small-python-programming
4Tags: short, math"""
5
6import sys
7
8print('''Fibonacci Sequence, by Al Sweigart al@inventwithpython.com
9
10The Fibonacci sequence begins with 0 and 1, and the next number is the
11sum of the previous two numbers. The sequence continues forever:
12
130, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987...
14''')
15
16while True: # Main program loop.
17 while True: # Keep asking until the user enters valid input.
18 print('Enter the Nth Fibonacci number you wish to')
19 print('calculate (such as 5, 50, 1000, 9999), or QUIT to quit:')
20 response = input('> ').upper()
21
22 if response == 'QUIT':
23 print('Thanks for playing!')
24 sys.exit()
25
26 if response.isdecimal() and int(response) != 0:
27 nth = int(response)
28 break # Exit the loop when the user enteres a valid number.
29
30 print('Please enter a number greater than 0, or QUIT.')
31 print()
32
33 # Handle the special cases if the user entered 1 or 2:
34 if nth == 1:
35 print('0')
36 print()
37 print('The #1 Fibonacci number is 0.')
38 continue
39 elif nth == 2:
40 print('0, 1')
41 print()
42 print('The #2 Fibonacci number is 1.')
43 continue
44
45 # Display warning if the user entered a large number:
46 if nth >= 10000:
47 print('WARNING: This will take a while to display on the')
48 print('screen. If you want to quit this program before it is')
49 print('done, press Ctrl-C.')
50 input('Press Enter to begin...')
51
52 # Calculate the Nth Fibonacci number:
53 secondToLastNumber = 0
54 lastNumber = 1
55 fibNumbersCalculated = 2
56 print('0, 1, ', end='') # Display the first two Fibonacci numbers.
57
58 # Display all the later numbers of the Fibonacci sequence:
59 while True:
60 nextNumber = secondToLastNumber + lastNumber
61 fibNumbersCalculated += 1
62
63 # Display the next number in the sequence:
64 print(nextNumber, end='')
65
66 # Check if we've found the Nth number the user wants:
67 if fibNumbersCalculated == nth:
68 print()
69 print()
70 print('The #', fibNumbersCalculated, ' Fibonacci ',
71 'number is ', nextNumber, sep='')
72 break
73
74 # Print a comma in between the sequence numbers:
75 print(', ', end='')
76
77 # Shift the last two numbers:
78 secondToLastNumber = lastNumber
79 lastNumber = nextNumber