Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
982 B

  1. ```Python
  2. def fib(n):
  3. if n == 0 or n == 1:
  4. return n
  5. return fib(n-1) + fib(n-2)
  6. ```
  7. ![Recursive Definition](media/fibonacci/RecursiveDefinition.png)
  8. ```Python
  9. def fibHelper(n, a, b):
  10. if n == 0:
  11. return a
  12. elif n == 1:
  13. return b
  14. return fibHelper(n-1, b, a+b)
  15. def fibIterative(n):
  16. return fibHelper(n, 0, 1)
  17. ```
  18. ![Iterative Performance](media/fibonacci/Iterative.png)
  19. ```Python
  20. def multiply(a,b):
  21. product = [0,0,0]
  22. product[0] = a[0]*b[0] + a[1]*b[1]
  23. product[1] = a[0]*b[1] + a[1]*b[2]
  24. product[2] = a[1]*b[1] + a[2]*b[2]
  25. return product
  26. def power(l, k):
  27. if k == 1:
  28. return l
  29. temp = power(l, k//2)
  30. if k%2 == 0:
  31. return multiply(temp, temp)
  32. else:
  33. return multiply(l, multiply(temp, temp))
  34. def fibPower(n):
  35. l = [1,1,0]
  36. return power(l, n)[1]
  37. ```
  38. ![FibPower Performance](media/fibonacci/FibPower.png)
  39. ![FibPower Performance](media/fibonacci/FibPowerBigPicture.png)