Mian-Chowla sequence: Difference between revisions

→‎{{header|Haskell}}: Added a Haskell version, in parallel to Python & JS
(→‎{{header|JavaScript}}: Updated JS in parallel to Functional Python)
(→‎{{header|Haskell}}: Added a Haskell version, in parallel to Python & JS)
Line 648:
fmt.Println(mc[90:100])
}</lang>
 
=={{header|Haskell}}==
{{Trans|Python}}
{{Trans|JavaScript}}
<lang haskell>import Data.Set (Set, fromList, insert, member)
 
mianChowlas :: Int -> [Int]
mianChowlas n =
let (_, cm, _) = unzip3 $ 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 = foldr (\m bln -> (bln && not (member (x + m) sumSet))) True mcs
m = until valid succ n
in (foldr insert sumSet ((2 * m) : fmap (m +) mcs), m : mcs, m)
 
main :: IO ()
main =
(putStrLn . unlines)
[ "First 30 terms of the Mian-Chowla series:"
, show (mianChowlas 30)
, []
, "Terms 91 to 100 of the Mian-Chowla series:"
, show $ drop 90 (mianChowlas 100)
]</lang>
{{Out}}
<pre>First 30 terms of the Mian-Chowla series:
[1,2,4,8,13,21,31,45,66,81,97,123,148,182,204,252,290,361,401,475,565,593,662,775,822,916,970,1016,1159,1312]
 
Terms 91 to 100 of the Mian-Chowla series:
[22526,23291,23564,23881,24596,24768,25631,26037,26255,27219]</pre>
 
 
=={{header|JavaScript}}==
9,655

edits