Last active
August 29, 2015 14:21
-
-
Save lenguyenthedat/e8ed6974fd5c8326972c to your computer and use it in GitHub Desktop.
Randomly generated a list of n 16-bit Integers, sum up their set bits.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Environment (getArgs) | |
import System.Random (StdGen, newStdGen, random) | |
import Data.Bits (popCount) | |
import Data.List (unfoldr) | |
import Data.Int (Int16) | |
main = do | |
seed <- newStdGen | |
args <- getArgs | |
let size = read $ head args | |
let rands = randomlist size seed | |
print $ sum $ map popCount rands | |
randomlist :: Int -> StdGen -> [Int16] | |
randomlist n = take n . unfoldr (Just . random) | |
-- $ runhaskell set-bits-int16.hs 1000000 | |
-- 8000183 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment