Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,10 @@
from math import *
def analytic_fibonacci(n):
sqrt_5 = sqrt(5);
p = (1 + sqrt_5) / 2;
q = 1/p;
return int( (p**n + q**n) / sqrt_5 + 0.5 )
for i in range(1,31):
print analytic_fibonacci(i),

View 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

View file

@ -0,0 +1,8 @@
F = {0: 0, 1: 1, 2: 1}
def fib(n):
if n in F:
return F[n]
f1 = fib(n // 2 + 1)
f2 = fib((n - 1) // 2)
F[n] = (f1 * f1 + f2 * f2 if n & 1 else f1 * f1 - f2 * f2)
return F[n]

View 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)]

View file

@ -0,0 +1,11 @@
from itertools import islice
def fib():
yield 0
yield 1
a, b = fib(), fib()
next(b)
while True:
yield next(a)+next(b)
print(tuple(islice(fib(), 10)))

View file

@ -0,0 +1,31 @@
'''Fibonacci accumulation'''
from itertools import accumulate, chain
from operator import add
# fibs :: Integer :: [Integer]
def fibs(n):
'''An accumulation of the first n integers in
the Fibonacci series. The accumulator is a
pair of the two preceding numbers.
'''
def go(ab, _):
return ab[1], add(*ab)
return [xy[1] for xy in accumulate(
chain(
[(0, 1)],
range(1, n)
),
go
)]
# MAIN ---
if __name__ == '__main__':
print(
'First twenty: ' + repr(
fibs(20)
)
)

View file

@ -0,0 +1,21 @@
'''Nth Fibonacci term (by folding)'''
from functools import reduce
from operator import add
# nthFib :: Integer -> Integer
def nthFib(n):
'''Nth integer in the Fibonacci series.'''
def go(ab, _):
return ab[1], add(*ab)
return reduce(go, range(1, n), (0, 1))[1]
# MAIN ---
if __name__ == '__main__':
print(
'1000th term: ' + repr(
nthFib(1000)
)
)

View file

@ -0,0 +1,3 @@
def fib(n):
from functools import reduce
return reduce(lambda x, y: (x[1], x[0] + x[1]), range(n), (0, 1))[0]

View file

@ -0,0 +1,6 @@
fibseq = [1,1,]
fiblength = 21
for x in range(1,fiblength-1):
xcount = fibseq[x-1] + fibseq[x]
fibseq.append(xcount)
print(xcount)

View file

@ -0,0 +1,7 @@
fi1=fi2=fi3=1 # FIB Russia rextester.com/FEEJ49204
for da in range(1, 88): # Danilin
print("."*(20-len(str(fi3))), end=' ')
print(fi3)
fi3 = fi2+fi1
fi1 = fi2
fi2 = fi3

View file

@ -0,0 +1,8 @@
def fibIter(n):
if n < 2:
return n
fibPrev = 1
fib = 1
for _ in range(2, n):
fibPrev, fib = fib, fib + fibPrev
return fib

View file

@ -0,0 +1,5 @@
def fib(n,x=[0,1]):
for i in range(abs(n)-1): x=[x[1],sum(x)]
return x[1]*pow(-1,abs(n)-1) if n<0 else x[1] if n else 0
for i in range(-30,31): print fib(i),

View file

@ -0,0 +1,5 @@
def fibRec(n):
if n < 2:
return n
else:
return fibRec(n-1) + fibRec(n-2)

View file

@ -0,0 +1,11 @@
def fibMemo():
pad = {0:0, 1:1}
def func(n):
if n not in pad:
pad[n] = func(n-1) + func(n-2)
return pad[n]
return func
fm = fibMemo()
for i in range(1,31):
print fm(i),

View file

@ -0,0 +1,7 @@
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)

View file

@ -0,0 +1,5 @@
def fibGen(n):
a, b = 0, 1
while n>0:
yield a
a, b, n = b, a+b, n-1

View file

@ -0,0 +1,3 @@
>>> [i for i in fibGen(11)]
[0,1,1,2,3,5,8,13,21,34,55]

View 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