Question

In: Computer Science

Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The...

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

Solutions

Expert Solution

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]


Related Solutions

(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem solved with recursion. However, this can also be done more efficiently using dynamic programming. Create a program that uses dynamic programming techniques to list the first 30 Fibonacci numbers.
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
A Fibonacci sequence, is a sequence of numbers with the property that each number is the...
A Fibonacci sequence, is a sequence of numbers with the property that each number is the sum of the two preceding Fibonacci numbers, starting from 0 and 1. Fibonacci number are usually denoted by Fn, where Fn is the nth Fibonacci number. Thus Fn = 0, and Fn = 1, and Fn = Fn-1 + Fn-2, n ≥ 2. Here are the first few Fibonacci numbers: F0=0 (by definition) F1=1 (by definition) F2 = F1 + F0 = 1 +...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally,...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2 Write a multithreaded C++ program that generates the Fibonacci series using the pthread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program will generate. The program will then create a separate thread that will...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics and applications to arrangement of flower petals, population growth of rabbits, and genetics. For each natural number n ≥ 1, the nth Fibonacci number fn is defined inductively by f1 = 1, f2 = 2, and fn+2 = fn+1 + fn (a) Compute the first 8 Fibonacci numbers f1, · · · , f8. (b) Show that for all natural numbers n, if α...
The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being...
The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being that the Lucas Numbers start with L0 = 2 and L1 = 1 as opposed to Fibonacci’s F0 = 0 and F1 = 1. Concretely, they are defined by L0 = 2, L1 = 1, and Ln := Ln−1 + Ln−2 for n > 1. Write a function in C++ that takes an integer argument N and returns the sum of the first N...
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following...
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following questions:          (a) Test to see if the leading digits conform to Benford’s law. Do this both graphically and analytically.          (b) Using the first 10 odd numbers in the sequence as sample 1 and the first 10 even numbers in the sequence as sample 2, use Wilcoxon’s Rank-Sum to test the claim that the numbers come from different populations.          (c) Repeat (b)...
Using python coding, test for convergence of an infinite sequence or series. keep the coding at...
Using python coding, test for convergence of an infinite sequence or series. keep the coding at beginner level please!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT