Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Mian-Chowla-sequence/Haskell/mian-chowla-sequence.hs
Normal file
24
Task/Mian-Chowla-sequence/Haskell/mian-chowla-sequence.hs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import Data.Set (Set, fromList, insert, member)
|
||||
|
||||
------------------- MIAN-CHOWLA SEQUENCE -----------------
|
||||
mianChowlas :: Int -> [Int]
|
||||
mianChowlas =
|
||||
reverse . snd . (iterate nextMC (fromList [2], [1]) !!) . subtract 1
|
||||
|
||||
nextMC :: (Set Int, [Int]) -> (Set Int, [Int])
|
||||
nextMC (sumSet, mcs@(n:_)) =
|
||||
(foldr insert sumSet ((2 * m) : fmap (m +) mcs), m : mcs)
|
||||
where
|
||||
valid x = not $ any (flip member sumSet . (x +)) mcs
|
||||
m = until valid succ n
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
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)
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue