Mian-Chowla sequence: Difference between revisions

→‎{{header|Haskell}}: third accumulator is just first element of second accumulator
(→‎{{header|Haskell}}: third accumulator is just first element of second accumulator)
Line 683:
mianChowlas :: Int -> [Int]
mianChowlas n =
let (_, cm, _) = unzip3unzip $ iterate nextMC (fromList [2], [1], 1)
in reverse $ cm !! (n - 1)
 
nextMC :: (Set Int, [Int], Int) -> (Set Int, [Int], Int)
nextMC (sumSet, mcs, @(n:_)) =
let valid x = all (not . flip member sumSet . (x +)) mcs
m = until valid succ n
in (foldr insert sumSet ((2 * m) : fmap (m +) mcs), m : mcs, m)
 
main :: IO ()
Anonymous user