2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1 +1 @@
|
|||
fib = 0 : 1 : zipWith (+) fib (tail fib)
|
||||
[floor(0.01+(1/p**n+p**n)/sqrt 5)|let p=(1+sqrt 5)/2, n<-[0..42]]
|
||||
|
|
|
|||
15
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-10.hs
Normal file
15
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-10.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import Data.List
|
||||
|
||||
xs <+> ys = zipWith (+) xs ys
|
||||
xs <*> ys = sum $ zipWith (*) xs ys
|
||||
|
||||
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 <*> ys | ys <- transpose $ unMat ym] | xs <- unMat xm]
|
||||
fromInteger n = Mat [[fromInteger n]]
|
||||
3
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-11.hs
Normal file
3
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-11.hs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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
|
||||
20
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-12.hs
Normal file
20
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-12.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
fibsteps (a,b) n
|
||||
| n <= 0 = (a,b)
|
||||
| otherwise = fibsteps (b, a+b) (n-1)
|
||||
|
||||
fibnums :: [Integer]
|
||||
fibnums = map fst $ iterate (`fibsteps` 1) (0,1)
|
||||
|
||||
fibN2 :: Integer -> (Integer, Integer)
|
||||
fibN2 m | m < 10 = fibsteps (0,1) 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) (*)
|
||||
2
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-13.hs
Normal file
2
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-13.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
*Main> take 10 $ show $ fst $ fibN2 (10^6)
|
||||
"1953282128"
|
||||
|
|
@ -1 +1 @@
|
|||
fib = 0 : 1 : next fib where next (a: t@(b:_)) = (a+b) : next t
|
||||
fib x = if x < 1 then 0 else if x < 2 then 1 else fib(x - 1) + fib(x - 2)
|
||||
|
|
|
|||
|
|
@ -1 +1,5 @@
|
|||
fib = 0 : scanl (+) 1 fib
|
||||
fib x = if x < 1 then 0
|
||||
else if x==1 then 1
|
||||
else fibs!!(x - 1) + fibs!!(x - 2)
|
||||
where
|
||||
fibs = map fib [0..]
|
||||
|
|
|
|||
|
|
@ -1,15 +1,2 @@
|
|||
import Data.List
|
||||
|
||||
xs <+> ys = zipWith (+) xs ys
|
||||
xs <*> ys = sum $ zipWith (*) xs ys
|
||||
|
||||
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 <*> ys | ys <- transpose $ unMat ym] | xs <- unMat xm]
|
||||
fromInteger n = Mat [[fromInteger n]]
|
||||
fib :: Integer -> Integer
|
||||
fib n = fst $ foldl (\(a, b) _ -> (b, a + b)) (0, 1) [1 .. n]
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
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
|
||||
fib n = go n 0 1
|
||||
where
|
||||
go n a b | n==0 = a
|
||||
| otherwise = go (n-1) b (a+b)
|
||||
|
|
|
|||
|
|
@ -1,20 +1 @@
|
|||
fibsteps (a,b) n
|
||||
| n <= 0 = (a,b)
|
||||
| otherwise = fibsteps (b, a+b) (n-1)
|
||||
|
||||
fibnums :: [Integer]
|
||||
fibnums = map fst $ iterate (`fibsteps` 1) (0,1)
|
||||
|
||||
fibN2 :: Integer -> (Integer, Integer)
|
||||
fibN2 m | m < 10 = fibsteps (0,1) 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
|
||||
d = ( 5*g^3 + if even n then (- 3*g) else 3*g ) -- 3(n+1) (*)
|
||||
b = ( g^3 + 3 * g * f^2 - f^3 ) -- 3n+1
|
||||
c = ( g^3 + 3 * g^2 * f + f^3 ) -- 3n+2
|
||||
fib = 0 : 1 : zipWith (+) fib (tail fib)
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
*Main> take 10 $ show $ fst $ fibN2 (10^6)
|
||||
"1953282128"
|
||||
fib = 0 : 1 : (zipWith (+) <*> tail) fib
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
let fib x = if x < 1 then 0 else (if x < 3 then 1 else (fib(x - 1) + fib(x - 2)))
|
||||
fib = 0 : 1 : next fib where next (a: t@(b:_)) = (a+b) : next t
|
||||
|
|
|
|||
1
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-9.hs
Normal file
1
Task/Fibonacci-sequence/Haskell/fibonacci-sequence-9.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
fib = 0 : scanl (+) 1 fib
|
||||
Loading…
Add table
Add a link
Reference in a new issue