Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,6 +1,8 @@
from math import *
def analytic_fibonacci(n):
assert isinstance(n,int), "n must be an integer."
assert n<=71 , "n must be <=71 due to floating point precision limitations."
sqrt_5 = sqrt(5);
p = (1 + sqrt_5) / 2;
q = 1/p;

View file

@ -1,7 +1,31 @@
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 prev_pow_two(n):
"""Gets the power of two that is less than or equal to the given input
"""
if ((n & -n) == n):
return n
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 crazy_fib(n):
"""Crazy fast fibonacci number calculation
"""
pow_two = prev_pow_two(n)
q = r = i = 1
s = 0
while i < pow_two:
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

View file

@ -1,8 +1,7 @@
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]
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

@ -1,10 +1,8 @@
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)]
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

@ -1,11 +1,11 @@
from itertools import islice
def fib():
yield 0
"""Yield fib[n+1] + fib[n]"""
yield 1
a, b = fib(), fib()
next(b)
lhs, rhs = fib(), fib()
# move lhs one iteration ahead
yield next(lhs)
while True:
yield next(a)+next(b)
yield next(lhs)+next(rhs)
print(tuple(islice(fib(), 10)))
f=fib()
print [next(f) for _ in range(9)]

View file

@ -1,23 +1,11 @@
'''Fibonacci accumulation'''
from itertools import islice
from itertools import accumulate
def fib():
yield 0
yield 1
a, b = fib(), fib()
next(b)
while True:
yield next(a)+next(b)
# 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.
'''
return [
a
for a, b in accumulate(
range(1, n), # we don't actually use these numbers
lambda acc, _: (acc[1], sum(acc)),
initial = (0, 1)
)
]
# MAIN ---
if __name__ == '__main__':
print(f'First twenty: {fibs(20)}')
print(tuple(islice(fib(), 10)))

View file

@ -1,18 +1,21 @@
'''Nth Fibonacci term (by folding)'''
def fibs(n):
"""Fibonacci accumulation
from functools import reduce
An accumulation of the first n integers in the Fibonacci series. The accumulator is a
pair of the two preceding numbers.
"""
# Local import is more efficient.
from itertools import accumulate
# nthFib :: Integer -> Integer
def nthFib(n):
'''Nth integer in the Fibonacci series.'''
return reduce(
lambda acc, _: (acc[1], sum(acc)),
# Note: Numbers generated in range(1, n) [or range(n-1)] call will not be used.
return [a for a, b in accumulate(
range(1, n),
(0, 1)
)[0]
lambda acc, _: (acc[1], sum(acc)),
initial = (0, 1)
)
]
# MAIN ---
if __name__ == '__main__':
n = 1000
print(f'{n}th term: {nthFib(n)}')
print(f'First twenty: {fibs(20)}')

View file

@ -1,3 +1,17 @@
def fib(n):
def nth_fib(n):
"""Nth Fibonacci term (by folding)
Nth integer in the Fibonacci series.
"""
from functools import reduce
return reduce(lambda x, y: (x[1], x[0] + x[1]), range(n), (0, 1))[0]
return reduce(
lambda acc, _: (acc[1], sum(acc)),
range(1, n),
(0, 1)
)[0]
# MAIN ---
if __name__ == '__main__':
n = 1000
print(f'{n}th term: {nth_fib(n)}')

View file

@ -1,6 +1,3 @@
fibseq = [1,1,]
fiblength = 21
for x in range(1,fiblength-1):
xcount = fibseq[x-1] + fibseq[x]
fibseq.append(xcount)
print(xcount)
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

@ -1,8 +1,28 @@
def fibIter(n):
if n < 2:
return n
fibPrev = 1
fib = 1
for _ in range(2, n):
fibPrev, fib = fib, fib + fibPrev
return fib
def analytic_fibonacci91(m):
"""
Binet's algebraic formula for the nth Fibonacci number.
Good for up to n=91
Uses numpy longdoubles:
See: https://artofproblemsolving.com/wiki/index.php/Binet%27s_Formula
"""
import numpy as np
assert isinstance(m,int), "parameter must be an integer."
assert 0<=m<=91 , "n must be in the range 0 .. 91 due to double precision floating point precision limitations."
if m < 2: return m
# Make sure that nothing causes conversion to single
n=np.longdouble(m)
C1=np.longdouble(1)
C2=np.longdouble(2)
C5=np.longdouble(5)
Chalf=C1/C2
Cfifth=C1/C5
root5=C5**Chalf
t1=(C1+root5)/C2
t2=(C1-root5)/C2
f=(t1**n-t2**n)/root5
return int(f+0.1)
# Usage
print(f:=[[i,analytic_fibonacci91(i)] for i in range(92)])

View file

@ -1,5 +1,60 @@
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
def fib4k(n): # FIBonacci's numbers, Binet's Formula, Barron's Binomial expansion, Kra's code.
# (c) 2025 David A. Kra GNUFDL1.3 and Copyleft Creative Commons CC BY-SA Attribution-ShareAlike
#
# ALL INTEGER. Tested up to Fib(4000).
# NOTE: This implementation is NOT faster than simply progressing up to Fib(n) by addition, starting from Fib(1).
# Thanks, Acknowledgement, and Appreciation to Barron https://stackexchange.com/users/9594318/barron
# No floats were exploited in the production of this function.
# References:
# https://math.stackexchange.com/questions/674570/prove-that-binets-formula-gives-an-integer-using-the-binomial-theorem
# https://math.stackexchange.com/questions/2002702/fibonacci-identity-with-binomial-coefficients
# https://artofproblemsolving.com/wiki/index.php/Binet%27s_Formula
# https://latex.artofproblemsolving.com/8/6/d/86d486c560727727342090b432e23ba85ac098b1.png
# https://www.geeksforgeeks.org/find-nth-fibonacci-number-using-binets-formula/
# https://discuss.geeksforgeeks.org/comment/540dc728-8cfc-41e3-853e-e920e0a85101/gfg
# # Fn=(1/(2**(n-1))) * SUM(j=0,n//2,((5**j)*comb(n,2*j+1))
#
# qc == Quick Combination. Instead of using comb, with its loop on each call,
# derive the next ( comb(n,2*j+1) ) by building up from what had already been calculated,
# Given comb(n,2*j+1), then
# comb(n,2*(j+1)+1) = comb(n,2*j+1) * (n-(2*j+1))*(n-2*j+1)-1) // ( (2*j+1)+1)*(2*j+1)+2) )
# qp == Quick Power. Instead of using 5**j, derive the next fttj as fttj*=5
#
f=0
fttj=1
qc=n # = comb(n,1)
for j in range(0,int(n/2+1)):
f+=fttj*qc # (5**k)*qc # qc == comb(n,2*k+1)
j2p1=j*2+1
# calculate the 5**k and the combinations for the next iteration,
# but for now, k and k2p1 have this iteration's values.
fttj*=5
qc=qc* (n-j2p1)*(n-j2p1-1) // ( (j2p1+1)*(j2p1+2) ) # for the next iteration
for i in range(-30,31): print fib(i),
f=f//(2 ** (n-1) )
return f
</syntaxhighlight lang="python">
<pre>
Test
print([[i,fib4k(i)] for i in [4,40,400,4000]])
output
[[4, 3], [40, 102334155], [400, 176023680645013966468226945392411250770384383304492191886725992896575345044216019675], [4000, 39909473435004422792081248094960912600792570982820257852628876326523051818641373433549136769424132442293969306537520118273879628025443235370362250955435654171592897966790864814458223141914272590897468472180370639695334449662650312874735560926298246249404168309064214351044459077749425236777660809226095151852052781352975449482565838369809183771787439660825140502824343131911711296392457138867486593923544177893735428602238212249156564631452507658603400012003685322984838488962351492632577755354452904049241294565662519417235020049873873878602731379207893212335423484873469083054556329894167262818692599815209582517277965059068235543139459375028276851221435815957374273143824422909416395375178739268544368126894240979135322176080374780998010657710775625856041594078495411724236560242597759185543824798332467919613598667003025993715274875]]
</pre>
===Iterative===
<syntaxhighlight lang="python">def fib_iter(n):
if n < 2:
return n
fib_prev = 1
fib = 1
for _ in range(2, n):
fib_prev, fib = fib, fib + fib_prev
return fib

View file

@ -1,5 +1,5 @@
def fibRec(n):
if n < 2:
return n
else:
return fibRec(n-1) + fibRec(n-2)
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

@ -1,11 +1,4 @@
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),
def fib_rec(n):
if n < 2:
return n
return fib_rec(n-1) + fib_rec(n-2)

View file

@ -1,7 +1,11 @@
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)
def fib_memo():
pad = {0:0, 1:1}
def sub_func(n):
if not n in pad:
pad[n] = sub_func(n-1) + sub_func(n-2)
return pad[n]
return sub_func
fm = fib_memo()
for i in range(1,31):
print(fm(i))

View file

@ -1,5 +1,6 @@
def fibGen(n):
a, b = 0, 1
while n>0:
yield a
a, b, n = b, a+b, n-1
def fib_fast_rec(n):
def inner_fib(prvprv, prv, c):
if c < 1:
return prvprv
return inner_fib(prv, prvprv + prv, c - 1)
return inner_fib(0, 1, n)

View file

@ -1,3 +1,5 @@
>>> [i for i in fibGen(11)]
[0,1,1,2,3,5,8,13,21,34,55]
def fib_gen(n):
a, b = 0, 1
while n>0:
yield a
a, b, n = b, a+b, n-1

View file

@ -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 fib_gen(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]