Data update

This commit is contained in:
Ingy döt Net 2025-02-27 18:35:13 -05:00
parent 8e4e15fa56
commit 72eb4943cb
1853 changed files with 35514 additions and 9441 deletions

View file

@ -1,35 +1,17 @@
import Control.Arrow ((&&&))
import Data.Ratio (numerator)
fibstep :: (Integer, Integer) -> (Integer, Integer)
fibstep (a, b) = (b, a + b)
infixl 7 *.
(*.) :: Num a => a -> [a] -> [a]
x *. (p:ps) = x*p : x*.ps
fibnums :: [Integer]
fibnums = map fst $ iterate fibstep (0, 1)
instance Num a => Num [a] where
negate = map negate
(+) = zipWith (+)
(*) (p:ps) (q:qs) = p*q : ((p*.qs) + ps*(q:qs))
fromInteger n = fromInteger n:repeat 0
fibN2 :: Integer -> (Integer, Integer)
fibN2 m
| m < 10 = iterate fibstep (0, 1) !! fromIntegral m
fibN2 m = fibN2_next (n, r) (fibN2 n)
where
(n, r) = quotRem m 3
instance (Eq a, Fractional a) => Fractional [a] where
(/) (0:ps) (0:qs) = ps/qs
(/) (p:ps) (q:qs) = let r=p/q in r : (ps - r*.qs)/(q:qs)
fibN2_next (n, r) (f, g)
| r == 0 = (a, b) -- 3n ,3n+1
| r == 1 = (b, c) -- 3n+1,3n+2
| r == 2 = (c, d) -- 3n+2,3n+3 (*)
where
a =
5 * f ^ 3 +
if even n
then 3 * f
else (-3 * f) -- 3n
b = g ^ 3 + 3 * g * f ^ 2 - f ^ 3 -- 3n+1
c = g ^ 3 + 3 * g ^ 2 * f + f ^ 3 -- 3n+2
d =
5 * g ^ 3 +
if even n
then (-3 * g)
else 3 * g -- 3(n+1) (*)
main :: IO ()
main = print $ (length &&& take 20) . show . fst $ fibN2 (10 ^ 2)
fromRational q = fromRational q:repeat 0

View file

@ -1,2 +1,3 @@
*Main> (length &&& take 20) . show . fst $ fibN2 (10^6)
(208988,"19532821287077577316")
fibs :: [Integer]
fibs = map numerator
(1/(1 : (-1) : (-1) : repeat 0))

View file

@ -1 +1,2 @@
f (n,(a,b)) = (2*n,(a*a+b*b,2*a*b+b*b)) -- iterate f (1,(0,1)) ; b is nth
ghci> take 15 fibs
[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]

View file

@ -1 +1,8 @@
g (n,(a,b)) = (2*n,(2*a*b-a*a,a*a+b*b)) -- iterate g (1,(1,1)) ; a is nth
import Data.Functor.Identity (Identity (..))
fibs :: [Integer]
fibs = runIdentity (hsequence (repeat f))
where f [] = Identity 1
f [_] = Identity 1
f xs = Identity ((xs !! (i-1)) + (xs !! i))
where i = length xs-1

View file

@ -0,0 +1,6 @@
hsequence :: Monad m => [[x] -> m x] -> m [x]
hsequence [] = pure []
hsequence (r:rs) = do
x <- r []
xs <- hsequence [ \ys -> g (x:ys) | g <- rs ]
pure (x:xs)

View file

@ -0,0 +1,2 @@
ghci> take 17 fibs
[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597]

View file

@ -0,0 +1,35 @@
import Control.Arrow ((&&&))
fibstep :: (Integer, Integer) -> (Integer, Integer)
fibstep (a, b) = (b, a + b)
fibnums :: [Integer]
fibnums = map fst $ iterate fibstep (0, 1)
fibN2 :: Integer -> (Integer, Integer)
fibN2 m
| m < 10 = iterate fibstep (0, 1) !! fromIntegral m
fibN2 m = fibN2_next (n, r) (fibN2 n)
where
(n, r) = quotRem m 3
fibN2_next (n, r) (f, g)
| r == 0 = (a, b) -- 3n ,3n+1
| r == 1 = (b, c) -- 3n+1,3n+2
| r == 2 = (c, d) -- 3n+2,3n+3 (*)
where
a =
5 * f ^ 3 +
if even n
then 3 * f
else (-3 * f) -- 3n
b = g ^ 3 + 3 * g * f ^ 2 - f ^ 3 -- 3n+1
c = g ^ 3 + 3 * g ^ 2 * f + f ^ 3 -- 3n+2
d =
5 * g ^ 3 +
if even n
then (-3 * g)
else 3 * g -- 3(n+1) (*)
main :: IO ()
main = print $ (length &&& take 20) . show . fst $ fibN2 (10 ^ 2)

View file

@ -0,0 +1,2 @@
*Main> (length &&& take 20) . show . fst $ fibN2 (10^6)
(208988,"19532821287077577316")

View file

@ -0,0 +1 @@
f (n,(a,b)) = (2*n,(a*a+b*b,2*a*b+b*b)) -- iterate f (1,(0,1)) ; b is nth

View file

@ -0,0 +1 @@
g (n,(a,b)) = (2*n,(2*a*b-a*a,a*a+b*b)) -- iterate g (1,(1,1)) ; a is nth

View file

@ -1,3 +1,3 @@
val fibonacci = fn x:if(x < 2: x ; fn((x - 1)) + fn((x - 2)))
writeln map(fibonacci, series(2..20))
writeln map(series(2..20), by=fibonacci)

View file

@ -1,8 +1,6 @@
'''Fibonacci accumulation'''
from itertools import accumulate, chain
from operator import add
from itertools import accumulate
# fibs :: Integer :: [Integer]
def fibs(n):
@ -10,22 +8,16 @@ def fibs(n):
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
)]
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(
'First twenty: ' + repr(
fibs(20)
)
)
print(f'First twenty: {fibs(20)}')

View file

@ -1,21 +1,18 @@
'''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]
return reduce(
lambda acc, _: (acc[1], sum(acc)),
range(1, n),
(0, 1)
)[0]
# MAIN ---
if __name__ == '__main__':
print(
'1000th term: ' + repr(
nthFib(1000)
)
)
n = 1000
print(f'{n}th term: {nthFib(n)}')

View file

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

@ -1,4 +1,4 @@
!yamlscript/v0
!YS-v0
defn main(n=10):
loop a 0, b 1, i 1: