June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,44 +1 @@
import Data.List (transpose)
fib
:: (Integral b, Num a)
=> b -> a
fib 0 = 0 -- this line is necessary because "something ^ 0" returns "fromInteger 1", which unfortunately
-- in our case is not our multiplicative identity (the identity matrix) but just a 1x1 matrix of 1
fib n = (last . head . unMat) (Mat [[1, 1], [1, 0]] ^ n)
-- Code adapted from Matrix exponentiation operator task ---------------------
(<+>)
:: Num c
=> [c] -> [c] -> [c]
(<+>) = zipWith (+)
(<*>)
:: Num a
=> [a] -> [a] -> a
(<*>) = (sum .) . zipWith (*)
newtype Mat a = Mat
{ unMat :: [[a]]
} deriving (Eq)
instance Show a =>
Show (Mat a) where
show xm = "Mat " ++ show (unMat xm)
instance Num a =>
Num (Mat a) where
negate xm = Mat $ map (map negate) $ unMat xm
xm + ym = Mat $ zipWith (<+>) (unMat xm) (unMat ym)
xm * ym =
Mat
[ [ xs Main.<*> ys -- to distinguish from standard applicative operator
| ys <- transpose $ unMat ym ]
| xs <- unMat xm ]
fromInteger n = Mat [[fromInteger n]]
abs = undefined
signum = undefined
-- TEST ----------------------------------------------------------------------
main :: IO ()
main = (print . take 10 . show . fib) (10 ^ 5)
fib = 0 : 1 : (zipWith (+) <*> tail) fib

View file

@ -1,35 +1 @@
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)
fib = 0 : 1 : next fib where next (a: t@(b:_)) = (a+b) : next t

View file

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

View file

@ -1 +1,9 @@
f (n,(a,b)) = (2*n,(a*a+b*b,2*a*b+b*b)) -- iterate f (1,(0,1)) ; b is nth
import Data.List (foldl') --'
fib :: Integer -> Integer
fib n =
fst $
foldl' --'
(\(a, b) _ -> (b, a + b))
(0, 1)
[1 .. n]

View file

@ -1 +1,44 @@
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.List (transpose)
fib
:: (Integral b, Num a)
=> b -> a
fib 0 = 0 -- this line is necessary because "something ^ 0" returns "fromInteger 1", which unfortunately
-- in our case is not our multiplicative identity (the identity matrix) but just a 1x1 matrix of 1
fib n = (last . head . unMat) (Mat [[1, 1], [1, 0]] ^ n)
-- Code adapted from Matrix exponentiation operator task ---------------------
(<+>)
:: Num c
=> [c] -> [c] -> [c]
(<+>) = zipWith (+)
(<*>)
:: Num a
=> [a] -> [a] -> a
(<*>) = (sum .) . zipWith (*)
newtype Mat a = Mat
{ unMat :: [[a]]
} deriving (Eq)
instance Show a =>
Show (Mat a) where
show xm = "Mat " ++ show (unMat xm)
instance Num a =>
Num (Mat a) where
negate xm = Mat $ map (map negate) $ unMat xm
xm + ym = Mat $ zipWith (<+>) (unMat xm) (unMat ym)
xm * ym =
Mat
[ [ xs Main.<*> ys -- to distinguish from standard applicative operator
| ys <- transpose $ unMat ym ]
| xs <- unMat xm ]
fromInteger n = Mat [[fromInteger n]]
abs = undefined
signum = undefined
-- TEST ----------------------------------------------------------------------
main :: IO ()
main = (print . take 10 . show . fib) (10 ^ 5)

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,5 +1,6 @@
fib n = go n 0 1
where
go n a b
| n == 0 = a
| otherwise = go (n - 1) b (a + b)
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo f where
f 0 = 0
f 1 = 1
f n = fib (n-1) + fib (n-2)

View file

@ -1 +1,6 @@
fib = 0 : 1 : zipWith (+) fib (tail fib)
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo $ \x -> case x of
0 -> 0
1 -> 1
n -> fib (n-1) + fib (n-2)

View file

@ -1 +1,7 @@
fib = 0 : 1 : (zipWith (+) <*> tail) fib
{-# Language LambdaCase #-}
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo $ \case
0 -> 0
1 -> 1
n -> fib (n-1) + fib (n-2)

View file

@ -1 +1,8 @@
fib = 0 : 1 : next fib where next (a: t@(b:_)) = (a+b) : next t
{-# Language LambdaCase #-}
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo $ \case
0 -> 0
1 -> 1
n | n>0 -> fib (n-1) + fib (n-2)
| otherwise -> fib (n+2) - fib (n+1)

View file

@ -1 +1,5 @@
fib = 0 : scanl (+) 1 fib
fib n = go n 0 1
where
go n a b
| n == 0 = a
| otherwise = go (n - 1) b (a + b)

View file

@ -1,9 +1 @@
import Data.List (foldl') --'
fib :: Integer -> Integer
fib n =
fst $
foldl' --'
(\(a, b) _ -> (b, a + b))
(0, 1)
[1 .. n]
fib = 0 : 1 : zipWith (+) fib (tail fib)