Question

In: Computer Science

Please write the following Python program. Also, show all output work. Computing the Fibonacci and Lucas...

Please write the following Python program.

Also, show all output work.

Computing the Fibonacci and Lucas Series¶

Goal:¶

The Fibonacci Series is a numeric series starting with the integers 0 and 1.

In this series, the next integer is determined by summing the previous two.

This gives us:

0, 1, 1, 2, 3, 5, 8, 13, ...

We will write a function that computes this series – then generalize it.

Step 1¶

Create a new module series.py in the session02 folder in your student folder.

In it, add a function called fibonacci.

The function should have one parameter n.

The function should return the nth value in the fibonacci series.

Ensure that your function has a well-formed docstring

Note that the fibinacci series is naturally recusive – the value is defined by previous values:

fib(n) = fib(n-2) + fib(n-1)

Lucas Numbers¶

The Lucas Numbers are a related series of integers that start with the values 2 and 1 rather than 0 and 1. The resulting series looks like this:

2, 1, 3, 4, 7, 11, 18, 29, ...

In your series.py module, add a new function lucas that returns the nth value in the lucas numbers series.

Ensure that your function has a well-formed docstring

Generalizing¶

Both the fibonacci series and the lucas numbers are based on an identical formula.

Add a third function called sum_series with one required parameter and two optional parameters. The required parameter will determine which element in the series to print. The two optional parameters will have default values of 0 and 1 and will determine the first two values for the series to be produced.

Calling this function with no optional parameters will produce numbers from the fibonacci series. Calling it with the optional arguments 2 and 1 will produce values from the lucas numbers. Other values for the optional parameters will produce other series.

Note: While you could check the input arguments, and then call one of the functions you wrote, the idea of this exercise is to make a general function, rather than one specialized. So you should re-impliment the code in this function.

In fact, you could go back and re-impliment your fibonacci and lucas functions to call this one with particular arguments.

Ensure that your function has a well-formed docstring

Tests...¶

Add a block of code to the end of your series.py module. Use the block to write a series of assert statements that demonstrate that your three functions work properly.

Use comments in this block to inform the observer what your tests do.

Add your new module to your git clone and commit frequently while working on your implementation. Include good commit messages that explain concisely both what you are doing and why.

When you are finished, push your changes to your fork of the class repository in GitHub and make a pull request.

Solutions

Expert Solution

## function for fabonicci series

def fabonicci(n):
if n == 0: return 0
elif n == 1: return 1
else: return fabonicci(n-1)+fabonicci(n-2)

## function for lucas series

def lucas(n):
if n == 0: return 2
elif n == 1: return 1
else: return lucas(n-1)+lucas(n-2)

##function for sum series which works as fabonicci or lucas based on the optional parameters

def sum_series(n,x=0,y=1):
if n == 0: return x
elif n == 1: return y
else: return sum_series(n-1,x,y)+sum_series(n-2,x,y)


## testing


## fabonicci series:
assert (fabonicci(0) == 0),"InCorrect"
assert (fabonicci(1) == 1),"InCorrect"
assert (fabonicci(2) == 1),"InCorrect"
assert (fabonicci(3) == 2),"InCorrect"
assert (fabonicci(4) == 3),"InCorrect"
assert (fabonicci(5) == 5),"InCorrect"
assert (fabonicci(6) == 8),"InCorrect"
assert (fabonicci(7) == 13),"InCorrect"

## lucas series:

assert (lucas(0) == 2),"InCorrect"
assert (lucas(1) == 1),"InCorrect"
assert (lucas(2) == 3),"InCorrect"
assert (lucas(3) == 4),"InCorrect"
assert (lucas(4) == 7),"InCorrect"
assert (lucas(5) == 11),"InCorrect"
assert (lucas(6) == 18),"InCorrect"
assert (lucas(7) == 29),"InCorrect"

##sum series: with default parameters

assert (sum_series(0) == 0),"InCorrect"
assert (sum_series(1) == 1),"InCorrect"
assert (sum_series(2) == 1),"InCorrect"
assert (sum_series(3) == 2),"InCorrect"
assert (sum_series(4) == 3),"InCorrect"
assert (sum_series(5) == 5),"InCorrect"
assert (sum_series(6) == 8),"InCorrect"
assert (sum_series(7) == 13),"InCorrect"

##sum series: with optional parameters

assert (sum_series(0,2,1) == 2),"InCorrect"
assert (sum_series(1,2,1) == 1),"InCorrect"
assert (sum_series(2,2,1) == 3),"InCorrect"
assert (sum_series(3,2,1) == 4),"InCorrect"
assert (sum_series(4,2,1) == 7),"InCorrect"
assert (sum_series(5,2,1) == 11),"InCorrect"
assert (sum_series(6,2,1) == 18),"InCorrect"
assert (sum_series(7,2,1) == 29),"InCorrect"

this code asserts nothing as all are correct

you can test more yourself if needed

or if you cant understand anything please ask or comment


Related Solutions

Please show all work an explain to be upvoted. Write an assembly program to convert an...
Please show all work an explain to be upvoted. Write an assembly program to convert an 8 character string from upper case to lower case.
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the...
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the 'nxt' method. In this class, the 'val' member is a Fibonacci number. The 'nxt' method will return a 'Fibonacci' object whose value is the next number in Fibonacci series. class Fibonacci (): """A Fibonacci number. >>>a = Fibonacci(): >>>a 0 >>>a.nxt() 1 >>>a.nxt().nxt() 1 >>>a.nxt().nxt().nxt() 2 >>>a.nxt().nxt().nxt().nxt() 3 >>>a.nxt().nxt().nxt().nxt().nxt() 5 >>>a.nxt.nxt().nxt().nxt().nxt().nxt() 8 """ def __init__(self): self.val = 0 def nxt(self): """YOUR SOURCE CODE HERE"""...
please write simple python 3 program with explanations and correct output Bilbo and problems on the...
please write simple python 3 program with explanations and correct output Bilbo and problems on the board Problem Statement Bilbo once reached his maths claws earlier than anyone else.He saw some N unique numbers greater than 0 written on the board.He thought of challenging his classmates when they come.So for each number i,he wrote the number before and after it on a paper slip.If it is the last number then the number after it is assumed to be 0.If it...
PLEASE WRITE IN PYTHON A sequence of integers is said to be Fibonacci-like if each element...
PLEASE WRITE IN PYTHON A sequence of integers is said to be Fibonacci-like if each element of the sequence (except the first two elements) is the sum of the previous two integers in the sequence. For example, the sequence 10, 14, 24, 38, 62, 100, 162, 262 is Fibonacci-like. Note that the first two integers in the above sequence are arbitrary. Each of the remaining integers is the sum of the two integers just before it in the sequence. For...
In Python Find the errors, debug the program, and then execute to show the output. def...
In Python Find the errors, debug the program, and then execute to show the output. def main():     Calories1 = input( "How many calories are in the first food?")     Calories2 = input( "How many calories are in the first food?")     showCalories(calories1, calories2)    def showCalories():     print('The total calories you ate today', format(calories1 + calories2,'.2f'))
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
IN ASSEMLY LANGUAGE MASM! please show output run. Write a program that ask the user to...
IN ASSEMLY LANGUAGE MASM! please show output run. Write a program that ask the user to write a string and reverse a string using indirect addressing (may not use the stack - push/pop). The string will be given by the user and be up to 64 characters long. INCLUDE Irvine32.inc INCLUDE macros.inc MAX = 64 .data source BYTE MAX DUP('#'),0 destination BYTE LENGTHOF source DUP('*'),0 actual_length DWORD ? ask BYTE "Enter a String: ",0 .code main proc ; ask user...
Please answer the problem below for all parts. Please show all work and write clearly. Thanks....
Please answer the problem below for all parts. Please show all work and write clearly. Thanks. (Apply total probability and Bayes’ rules) A large industrial firm uses three local motels to provide overnight accommodations for its clients. From past experience it is known that 22% of the clients are assigned rooms at the Ramada Inn, 50% at the Sheraton, and 28% at the Lakeview Motor Lodge. If the plumbing is faulty in 5% of the rooms at the Ramada Inn,...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT