Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
30
Task/Fibonacci-sequence/Python/fibonacci-sequence-9.py
Normal file
30
Task/Fibonacci-sequence/Python/fibonacci-sequence-9.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
def prevPowTwo(n):
|
||||
'Gets the power of two that is less than or equal to the given input'
|
||||
if ((n & -n) == n):
|
||||
return n
|
||||
else:
|
||||
n -= 1
|
||||
n |= n >> 1
|
||||
n |= n >> 2
|
||||
n |= n >> 4
|
||||
n |= n >> 8
|
||||
n |= n >> 16
|
||||
n += 1
|
||||
return (n/2)
|
||||
|
||||
def crazyFib(n):
|
||||
'Crazy fast fibonacci number calculation'
|
||||
powTwo = prevPowTwo(n)
|
||||
|
||||
q = r = i = 1
|
||||
s = 0
|
||||
|
||||
while(i < powTwo):
|
||||
i *= 2
|
||||
q, r, s = q*q + r*r, r * (q + s), (r*r + s*s)
|
||||
|
||||
while(i < n):
|
||||
i += 1
|
||||
q, r, s = q+r, q, r
|
||||
|
||||
return q
|
||||
Loading…
Add table
Add a link
Reference in a new issue