Partition function P: Difference between revisions

m
→‎{{header|Haskell}}: Applied Ormolu
m (→‎{{header|Haskell}}: Applied Ormolu)
Line 660:
=={{header|Haskell}}==
 
<lang haskell>{-# languageLANGUAGE DeriveFunctor #-}
 
------------------------------------------------------------
Line 666:
 
data Memo a = Node a (Memo a) (Memo a)
deriving (Functor )
 
memo :: Integral a => Memo p -> a -> p
Line 675:
 
nats :: Memo Int
nats =
nats = Node 0 ((+1).(*2) <$> nats) ((*2).(+1) <$> nats)
Node
0
((+ 1) . (* 2) <$> nats)
nats = Node 0 ((+1).(* 2) <$> nats). ((*2).(+ 1) <$> nats)
 
------------------------------------------------------------
Line 688 ⟶ 692:
| otherwise = sum $ zipWith (*) signs terms
where
terms = [ memo partitions (n - i)
[ memo partitions (n |- i <- takeWhile (<= n) ofsets ]
| i <- takeWhile (<= n) ofsets
signs = cycle [1,1,-1,-1]
]
signs = cycle [1, 1, -1, -1]
 
ofsets = scanl1 (+) $ mix [1,3..]:: [1,2..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</lang>
 
9,655

edits