Browse Source

Created initial content for the new post on 3 ways to calculate the fibonacci sequence.

pull/2/head
jrtechs 6 years ago
parent
commit
e9cdec6482
5 changed files with 58 additions and 0 deletions
  1. +58
    -0
      blogContent/posts/programming/EverythingFibonacci.md
  2. BIN
      blogContent/posts/programming/media/fibonacci/FibPower.png
  3. BIN
      blogContent/posts/programming/media/fibonacci/FibPowerBigPicture.png
  4. BIN
      blogContent/posts/programming/media/fibonacci/Iterative.png
  5. BIN
      blogContent/posts/programming/media/fibonacci/RecursiveDefinition.png

+ 58
- 0
blogContent/posts/programming/EverythingFibonacci.md View File

@ -0,0 +1,58 @@
```Python
def fib(n):
if n == 0 or n == 1:
return n
return fib(n-1) + fib(n-2)
```
![Recursive Definition](media/fibonacci/RecursiveDefinition.png)
```Python
def fibHelper(n, a, b):
if n == 0:
return a
elif n == 1:
return b
return fibHelper(n-1, b, a+b)
def fibIterative(n):
return fibHelper(n, 0, 1)
```
![Iterative Performance](media/fibonacci/Iterative.png)
```Python
def multiply(a,b):
product = [0,0,0]
product[0] = a[0]*b[0] + a[1]*b[1]
product[1] = a[0]*b[1] + a[1]*b[2]
product[2] = a[1]*b[1] + a[2]*b[2]
return product
def power(l, k):
if k == 1:
return l
temp = power(l, k//2)
if k%2 == 0:
return multiply(temp, temp)
else:
return multiply(l, multiply(temp, temp))
def fibPower(n):
l = [1,1,0]
return power(l, n)[1]
```
![FibPower Performance](media/fibonacci/FibPower.png)
![FibPower Performance](media/fibonacci/FibPowerBigPicture.png)

BIN
blogContent/posts/programming/media/fibonacci/FibPower.png View File

Before After
Width: 640  |  Height: 480  |  Size: 38 KiB

BIN
blogContent/posts/programming/media/fibonacci/FibPowerBigPicture.png View File

Before After
Width: 640  |  Height: 480  |  Size: 31 KiB

BIN
blogContent/posts/programming/media/fibonacci/Iterative.png View File

Before After
Width: 640  |  Height: 480  |  Size: 26 KiB

BIN
blogContent/posts/programming/media/fibonacci/RecursiveDefinition.png View File

Before After
Width: 640  |  Height: 480  |  Size: 19 KiB

Loading…
Cancel
Save