Browse Source

Implemented the closed definition of fibonacci.

pull/2/head
jrtechs 2 years ago
parent
commit
9444d8432b
5 changed files with 22 additions and 5 deletions
  1. +7
    -0
      README.md
  2. BIN
      fibonacci/ClosedFormulaWide.png
  3. BIN
      fibonacci/ConstantTimeComplexity.png
  4. +11
    -1
      fibonacci/Fibonacci.py
  5. +4
    -4
      fibonacci/PlotTimeComplexity.py

+ 7
- 0
README.md View File

@@ -0,0 +1,7 @@
## Dependencies

### Matplotlib
```bash
python -m pip install -U pip
python -m pip install -U matplotlib
```

BIN
fibonacci/ClosedFormulaWide.png View File

Before After
Width: 1284  |  Height: 480  |  Size: 44 KiB

BIN
fibonacci/ConstantTimeComplexity.png View File

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

+ 11
- 1
fibonacci/Fibonacci.py View File

@@ -1,3 +1,6 @@
import math as math


def fib(n):
if n == 0 or n == 1:
return n
@@ -39,11 +42,18 @@ def fibPower(n):
return power(l, n)[1]



def fibClosedFormula(n):
p = ((1+ math.sqrt(5))/2)**n
v = ((1-math.sqrt(5))/2)**n
return (p-v)/math.sqrt(5)


"""
Makes sure that other programs don't execute the main
"""
if __name__ == '__main__':
try:
print(fib(30))
print(closedFormula(6))
except KeyboardInterrupt:
exit()

+ 4
- 4
fibonacci/PlotTimeComplexity.py View File

@@ -1,14 +1,14 @@
import matplotlib.pyplot as plt
import time
from fibonacci import Fibonacci as fib
import Fibonacci as fib


def measureTime(n):

total = 0
for i in range(0, 200):
for i in range(0, 200000):
start_time = time.time()
fib.fibPower(n)
fib.fibClosedFormula(n)
end_time = time.time()
total += end_time - start_time
return total/200
@@ -18,7 +18,7 @@ def generateData():
time = []
input = []

for i in range(1, 2000, 20):
for i in range(1, 1000, 20):
input.append(i)
time.append(measureTime(i))
return input, time


Loading…
Cancel
Save