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.

49 lines
861 B

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