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,5 @@
recursive = (n):
if (n <= 1): 1. else: recursive (n - 1) + recursive (n - 2)..
n = 40
("fib(", n, ")= ", recursive (n), "\n") join print

View file

@ -0,0 +1,11 @@
iterative = (n) :
curr = 0
prev = 1
tmp = 0
n times:
tmp = curr
curr = curr + prev
prev = tmp
.
curr
.

View file

@ -0,0 +1,20 @@
sqr = (x): x * x.
# Based on the fact that
# F2n = Fn(2Fn+1 - Fn)
# F2n+1 = Fn ^2 + Fn+1 ^2
matrix = (n) :
algorithm = (n) :
"computes (Fn, Fn+1)"
if (n < 2): return ((0, 1), (1, 1)) at(n).
# n = e + {0, 1}
q = algorithm(n / 2) # q = (Fe/2, Fe/2+1)
q = (q(0) * (2 * q(1) - q(0)), sqr(q(0)) + sqr(q(1))) # q => (Fe, Fe+1)
if (n % 2 == 1) : # q => (Fe+{0, 1}, Fe+1+{0,1}) = (Fn, Fn+1)
q = (q(1), q(1) + q(0))
.
q
.
algorithm(n)(0)
.

View file

@ -0,0 +1,13 @@
fibonacci = (n) :
myFavorite = matrix
if (n >= 0) :
myFavorite(n)
. else :
n = n * -1
if (n % 2 == 1) :
myFavorite(n)
. else :
myFavorite(n) * -1
.
.
.