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,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