In: Computer Science
Using Python:
The Fibonacci sequence is a famous series of numbers with the following rules:
The first number in the sequence is 0 -
The second number in the sequence is 1 -
The other numbers in the sequence are composed by adding up the two previous numbers in the sequence. We therefore have the following sequence: 1 st number: 0 2nd number: 1 3 rd number: 0 + 1 = 1 4 th number: 1+1 =2 5 th number: 2+1 = 3 6 th number: 3 + 2 = 5 7 th number: 5 + 3 = 8 and so on….
a. Write a program that takes a value n from the user and , using recursive function, display the first n Fibonacci numbers. (3pts)
b. Then, using List Comprehension, indicate which of the first n Fibonacci numbers are (2pts) : - odd numbers - multiples of 5
Example: How many Fibonacci numbers do you want ? 2 The first Fibonacci numbers are : [0, 1] From this list, the odd numbers are : [1] There is no multiples of 5 in this list How many Fibonacci numbers do you want ? 5 The first Fibonacci numbers are : [0, 1, 1, 2, 3] From this list, the odd numbers are : [1, 1, 3] There is no multiples of 5 in this list How many Fibonacci numbers do you want ? 11 The first Fibonacci numbers are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] From this list, the odd numbers are : [1, 1, 3, 5, 13, 21, 55] From this list, the multiples of 5 are : [5, 55] Note that 0 is neither an odd number nor multiple of 5
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
#recursive method to find and return a list of first n
fibonacci numbers
#here current and prev parameters are used for recursion. The
calling
#program need not pass any values to these
def fibonacci(n,current=1, prev=0):
#if n is invalid or reached 0, returning
None (base condition)
if n<=0:
return
None
else:
#creating a list
containing prev element
data=[prev]
#finding remaining
list of fibonacci numbers, passing one less value for n,
#adding prev to current,
passing old current as new prev
remaining=fibonacci(n-1,current+prev,current)
#if value of
remaining is not None, appending the contents of remaining to
#data list. Python lists
have built in implementation for '+' and '+=' methods that
#concatenates two
lists
if
remaining is not None:
data+=remaining
#returning this
list
return data
#reading value for n
n=int(input("How many Fibonacci numbers do you want ?
"))
#finding a list of n fibonacci numbers
numbers=fibonacci(n)
#using a list comprehension, finding a list of odd numbers from
numbers list
odd=[i for i in numbers
if i>0 and i%2!=0]
#using a list comprehension, finding a list of multiples of 5
from numbers list
multiplesOf5=[i for i in numbers
if i>0 and i%5==0]
#printing numbers
print("The first {} Fibonacci numbers are
:".format(n))
print(numbers)
#printing odd numbers if it is not empty, else printing proper
messages
if len(odd)==0:
print("There are no odd numbers in this
list")
else:
print("From this list, the odd numbers
are :")
print(odd)
#printing list of multiples of 5 if it is not empty, else
printing proper messages
if len(multiplesOf5)==0:
print("There are no multiples of 5 in
this list")
else:
print("From this list, the multiples of
5 are :")
print(multiplesOf5)
#output
How many Fibonacci numbers do you want ? 10
The first 10 Fibonacci numbers are :
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
From this list, the odd numbers are :
[1, 1, 3, 5, 13, 21]
From this list, the multiples of 5 are :
[5]