diff --git a/fibonacci/FibPower.png b/fibonacci/FibPower.png new file mode 100644 index 0000000..0c4a8e4 Binary files /dev/null and b/fibonacci/FibPower.png differ diff --git a/fibonacci/FibPowerBigPicture.png b/fibonacci/FibPowerBigPicture.png new file mode 100644 index 0000000..53b0b31 Binary files /dev/null and b/fibonacci/FibPowerBigPicture.png differ diff --git a/fibonacci/Fibonacci.py b/fibonacci/Fibonacci.py new file mode 100644 index 0000000..5a40149 --- /dev/null +++ b/fibonacci/Fibonacci.py @@ -0,0 +1,49 @@ +def fib(n): + if n == 0 or n == 1: + return n + return fib(n-1) + fib(n-2) + + +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) + + +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] + + +""" +Makes sure that other programs don't execute the main +""" +if __name__ == '__main__': + try: + print(fib(30)) + except KeyboardInterrupt: + exit() diff --git a/fibonacci/Iterative.png b/fibonacci/Iterative.png new file mode 100644 index 0000000..d0a4ef0 Binary files /dev/null and b/fibonacci/Iterative.png differ diff --git a/fibonacci/PlotTimeComplexity.py b/fibonacci/PlotTimeComplexity.py new file mode 100644 index 0000000..25d59c1 --- /dev/null +++ b/fibonacci/PlotTimeComplexity.py @@ -0,0 +1,32 @@ +import matplotlib.pyplot as plt +import time +from fibonacci import Fibonacci as fib + + +def measureTime(n): + + total = 0 + for i in range(0, 200): + start_time = time.time() + fib.fibPower(n) + end_time = time.time() + total += end_time - start_time + return total/200 + + +def generateData(): + time = [] + input = [] + + for i in range(1, 2000, 20): + input.append(i) + time.append(measureTime(i)) + return input, time + + +data = generateData() +plt.plot(data[0], data[1]) +plt.xlabel('Fibonacci Term') +plt.ylabel('Seconds') + +plt.show() \ No newline at end of file diff --git a/fibonacci/RecursiveDefinition.png b/fibonacci/RecursiveDefinition.png new file mode 100644 index 0000000..ef37256 Binary files /dev/null and b/fibonacci/RecursiveDefinition.png differ