Nowadays, you have many influencers and trendsetters, popularizing things like fashion, music and media. However, that i

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Nowadays, you have many influencers and trendsetters, popularizing things like fashion, music and media. However, that i

Post by answerhappygod »

Nowadays, you have many influencers and trendsetters,
popularizing things like fashion, music and media. However, that is
nothing compared to one of the biggest trendsetters in the Western
history: Leonardo Bonacci, also known as Fibonacci.
In his book Liber Abaci, which the Italian (officially born in
the Republic of Pisa) mathematician wrote in 1202, he introduced
the Western world to the Hindu-Arabic numeral system. Until then,
Europeans still used the Roman numeral system, which made it almost
impossible to do modern mathematics. In his book, Fibonacci
advocated the use of the Hindu-Arabic numeral system: using the
digits 0 to 9 and positional notation. After his work, it still
took many centuries for this system to spread widely in the Western
world. Next to this, Fibonacci made another big contribution to
mathematics in his Liber Abaci: the Fibonacci sequence (a term
coined later by French mathematician Edouard Lucas). He discussed
the problem:
A certain man put a pair of
rabbits in a place surrounded on all sides by a wall. How many
pairs of rabbits can be produced from that pair in a year if it is
supposed that every month each pair begets a new pair which
from the second month on becomes productive?
If we assume that no rabbits die, and that the first pair breeds
immediately, we get the sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
89, 144, .... This is the Fibonacci sequence: generalized by the
fact that each new number is the sum of the previous two
numbers.
Let’s create a script, fibonacci.py, that asks the user to input
a non-negative integer n and prints n numbers of the Fibonacci
sequence. Also, add input validation and fail gracefully if the
user did not input a non-negative integer by printing “Please input
a non-negative integer.” to the screen. Put your Fibonacci
generator in a separate function called fibonacci(n), that takes as
input the number of elements to print, this function does not
return anything but rather prints the numbers directly to the
screen.
Put your input() statement, your input validation and the call
to this function again in the if __name__ == "__main__": block on
the bottom of the script.
Example usage:
$ python3 fibonacci.py Number of Fibonacci Sequence numbers:
4
1
1
2
3
$ python3 fibonacci.py Number of Fibonacci Sequence numbers:
10
1
1
2
3
5
8
13
21
34
55
$ python3 fibonacci.py
Number of Fibonacci Sequence numbers: 1 1
$ python3 fibonacci.py
Number of Fibonacci Sequence numbers: word
Please input a non-negative integer.
Please input a non-negative integer.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply