Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Partition-function-P/Haskell/partition-function-p.hs
Normal file
45
Task/Partition-function-P/Haskell/partition-function-p.hs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
{-# LANGUAGE DeriveFunctor #-}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- memoization utilities
|
||||
|
||||
data Memo a = Node a (Memo a) (Memo a)
|
||||
deriving (Functor)
|
||||
|
||||
memo :: Integral a => Memo p -> a -> p
|
||||
memo (Node a l r) n
|
||||
| n == 0 = a
|
||||
| odd n = memo l (n `div` 2)
|
||||
| otherwise = memo r (n `div` 2 - 1)
|
||||
|
||||
nats :: Memo Int
|
||||
nats =
|
||||
Node
|
||||
0
|
||||
((+ 1) . (* 2) <$> nats)
|
||||
((* 2) . (+ 1) <$> nats)
|
||||
|
||||
------------------------------------------------------------
|
||||
-- calculating partitions
|
||||
|
||||
partitions :: Memo Integer
|
||||
partitions = partitionP <$> nats
|
||||
|
||||
partitionP :: Int -> Integer
|
||||
partitionP n
|
||||
| n < 2 = 1
|
||||
| otherwise = sum $ zipWith (*) signs terms
|
||||
where
|
||||
terms =
|
||||
[ memo partitions (n - i)
|
||||
| i <- takeWhile (<= n) ofsets
|
||||
]
|
||||
signs = cycle [1, 1, -1, -1]
|
||||
|
||||
ofsets :: [Int]
|
||||
ofsets = scanl1 (+) $ mix [1, 3 ..] [1, 2 ..]
|
||||
where
|
||||
mix a b = concat $ zipWith (\x y -> [x, y]) a b
|
||||
|
||||
main :: IO ()
|
||||
main = print $ partitionP 6666
|
||||
Loading…
Add table
Add a link
Reference in a new issue