In: Computer Science
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.
## 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