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