fork(1) download
  1. # All the matrices used
  2. # in Fib generation
  3. # has the form:
  4. # [a b]
  5. # [b c]
  6.  
  7. # [a b] \/ [a b] -- [a*a+b*b a*b+b*c]
  8. # [b c] /\ [b c] -- [a*b+b*c b*b+c*c]
  9.  
  10. # F(2n) = (F(n-1) + F(n+1)) * F(n)
  11.  
  12. # [a b] \/ [1 1] -- [a+b a]
  13. # [b c] /\ [1 0] -- [b+c b]
  14.  
  15. def fast_matrix_pow(n):
  16. if n==0: return [1,0,1]
  17. [a,b,c] = fast_matrix_pow(n>>1)
  18. tmp = b*b
  19. a,b,c = a*a+tmp, (a+c)*b, tmp+c*c
  20. if n%2 == 1:
  21. return [a+b,a,b]
  22. else:
  23. return [a,b,c]
  24.  
  25. def fast_fib(n):
  26. return fast_matrix_pow(n)[1]
  27.  
  28. print ([[fast_fib(x) for x in range(71)] for _ in range(1000)][-1])
Success #stdin #stdout 0.53s 9936KB
stdin
Standard input is empty
stdout
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135]