Repository where I mostly put random python scripts.
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.

59 lines
1015 B

  1. import math as math
  2. def fib(n):
  3. if n == 0 or n == 1:
  4. return n
  5. return fib(n-1) + fib(n-2)
  6. def fibHelper(n, a, b):
  7. if n == 0:
  8. return a
  9. elif n == 1:
  10. return b
  11. return fibHelper(n-1, b, a+b)
  12. def fibIterative(n):
  13. return fibHelper(n, 0, 1)
  14. def multiply(a,b):
  15. product = [0,0,0]
  16. product[0] = a[0]*b[0] + a[1]*b[1]
  17. product[1] = a[0]*b[1] + a[1]*b[2]
  18. product[2] = a[1]*b[1] + a[2]*b[2]
  19. return product
  20. def power(l, k):
  21. if k == 1:
  22. return l
  23. temp = power(l, k//2)
  24. if k%2 == 0:
  25. return multiply(temp, temp)
  26. else:
  27. return multiply(l, multiply(temp, temp))
  28. def fibPower(n):
  29. l = [1,1,0]
  30. return power(l, n)[1]
  31. def fibClosedFormula(n):
  32. p = ((1+ math.sqrt(5))/2)**n
  33. v = ((1-math.sqrt(5))/2)**n
  34. return (p-v)/math.sqrt(5)
  35. """
  36. Makes sure that other programs don't execute the main
  37. """
  38. if __name__ == '__main__':
  39. try:
  40. print(closedFormula(6))
  41. except KeyboardInterrupt:
  42. exit()