Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
10
Task/Fibonacci-sequence/Python/fibonacci-sequence-10.py
Normal file
10
Task/Fibonacci-sequence/Python/fibonacci-sequence-10.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def fib():
|
||||
"""Yield fib[n+1] + fib[n]"""
|
||||
yield 1 # have to start somewhere
|
||||
lhs, rhs = fib(), fib()
|
||||
yield next(lhs) # move lhs one iteration ahead
|
||||
while True:
|
||||
yield next(lhs)+next(rhs)
|
||||
|
||||
f=fib()
|
||||
print [next(f) for _ in range(9)]
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
def fibGen():
|
||||
f0, f1 = 0, 1
|
||||
while True:
|
||||
yield f0
|
||||
f0, f1 = f1, f0+f1
|
||||
def fibFastRec(n):
|
||||
def fib(prvprv, prv, c):
|
||||
if c < 1: return prvprv
|
||||
else: return fib(prv, prvprv + prv, c - 1)
|
||||
return fib(0, 1, n)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,4 @@
|
|||
>>> fg = fibGen()
|
||||
>>> for x in range(9):
|
||||
print fg.next()
|
||||
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
3
|
||||
5
|
||||
8
|
||||
13
|
||||
21
|
||||
>>>
|
||||
def fibGen(n,a=0,b=1):
|
||||
while n>0:
|
||||
yield a
|
||||
a,b,n = b,a+b,n-1
|
||||
|
|
|
|||
|
|
@ -1,30 +1,3 @@
|
|||
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)
|
||||
>>> [i for i in fibGen(11)]
|
||||
|
||||
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
|
||||
[0,1,1,2,3,5,8,13,21,34,55]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,30 @@
|
|||
def fib(n, c={0:1, 1:1}):
|
||||
if n not in c:
|
||||
x = n // 2
|
||||
c[n] = fib(x-1) * fib(n-x-1) + fib(x) * fib(n - x)
|
||||
return c[n]
|
||||
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)
|
||||
|
||||
fib(10000000) # calculating it takes a few seconds, printing it takes eons
|
||||
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
|
||||
|
|
|
|||
7
Task/Fibonacci-sequence/Python/fibonacci-sequence-9.py
Normal file
7
Task/Fibonacci-sequence/Python/fibonacci-sequence-9.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def fib(n, c={0:1, 1:1}):
|
||||
if n not in c:
|
||||
x = n // 2
|
||||
c[n] = fib(x-1) * fib(n-x-1) + fib(x) * fib(n - x)
|
||||
return c[n]
|
||||
|
||||
fib(10000000) # calculating it takes a few seconds, printing it takes eons
|
||||
Loading…
Add table
Add a link
Reference in a new issue