Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook Fibonnacci_numbers.ipynb

Views: 105
Kernel: T - Python 3 (Ubuntu Linux)

The Task - create a program that prints the fibonacci numbers

Fibonacci numbers are a set of numbers where each sequential number is the sum of the previous two numbers... i.e. 1, 1, 2, 3, 5, 8, 13, etc.

I explore three different ways to code a solution. The first solution shown below takes an input x, and generates all fibonacci numbers between 1 and x.

First solution - switching the values around

""" My first solution was to use two variables: a and b, in order to shuffle values around, and then update a third value: 'result' which is the actual fibonacci number printed with each iteration. """ def fibo(x): a, b, result = 0, 1, 1 # a : current fibo iteration, b : previous fibo iteration while result <= x: # runs u print result, b = result # b stores the previous fibo number result = result + a # result updated to next fibo number a = b # increment a ready for next calculation
fibo(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

Streamlined version:

""" Here I have removed the result variable, moved a and b into the function definition as default values, and consolidated the juggling of figures onto one line. """ def fibo(x, a = 0, b = 1): while b <= x: print b, a, b = b, a + b
fibo(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

What if we wanted to produce a set number of fibonacci numbers, as opposed to all the fibo numbers up to a particular value?

Second solution - set number of iterations:

def fibo(x): nums = [0,1] # a list to hold the fibonacci numbers for i in range(1, x): # append the next fibonacci number to the list for the defined number of iterations 'x' nums.append(nums[-1] + nums[-2]) # by summing the two previous numbers print(" ".join(str(i) for i in nums[1:])) # print a string of the numbers from the list, avoiding the starting '0'
fibo(20)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Third solution - recursive:

"""Returns the nth fibonacci number via recursion.""" def fibo(n): if n == 1 or n == 2: # first two fibbo no.'s are 1. These end the recursion return 1 return fibo(n - 1) + fibo(n - 2) # recursively sum the two previous numbers
fibo(20)
6765

I would like to use the above recursive function to print x iterations, so I will define a separate function to do so:

def fiboPrint(x): for i in range(1, x + 1): print fibo(i),
fiboPrint(20)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

And finally...

I wish to make the output slightly more practical for the user. To clarify what the nth value is, I have slightly altered the FiboPrint function:

def fiboPrint(x): for i in range(1, x + 1): print "Iteration " + str(i) + " is: " + str(fibo(i))
fiboPrint(10)
Iteration 1 is: 1 Iteration 2 is: 1 Iteration 3 is: 2 Iteration 4 is: 3 Iteration 5 is: 5 Iteration 6 is: 8 Iteration 7 is: 13 Iteration 8 is: 21 Iteration 9 is: 34 Iteration 10 is: 55