diff --git a/README.md b/README.md new file mode 100644 index 0000000..42baa55 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +## Dependencies + +### Matplotlib +```bash +python -m pip install -U pip +python -m pip install -U matplotlib +``` \ No newline at end of file diff --git a/fibonacci/ClosedFormulaWide.png b/fibonacci/ClosedFormulaWide.png new file mode 100644 index 0000000..f5ae1bf Binary files /dev/null and b/fibonacci/ClosedFormulaWide.png differ diff --git a/fibonacci/ConstantTimeComplexity.png b/fibonacci/ConstantTimeComplexity.png new file mode 100644 index 0000000..2c781f4 Binary files /dev/null and b/fibonacci/ConstantTimeComplexity.png differ diff --git a/fibonacci/Fibonacci.py b/fibonacci/Fibonacci.py index 5a40149..d5afdce 100644 --- a/fibonacci/Fibonacci.py +++ b/fibonacci/Fibonacci.py @@ -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() diff --git a/fibonacci/PlotTimeComplexity.py b/fibonacci/PlotTimeComplexity.py index 25d59c1..e1adcf1 100644 --- a/fibonacci/PlotTimeComplexity.py +++ b/fibonacci/PlotTimeComplexity.py @@ -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