Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,10 @@
import Data.Bits (popCount)
printPops :: (Show a, Integral a) => String -> [a] -> IO ()
printPops title counts = putStrLn $ title ++ show (take 30 counts)
main :: IO ()
main = do
printPops "popcount " $ map popCount $ iterate (*3) (1 :: Integer)
printPops "evil " $ filter (even . popCount) ([0..] :: [Integer])
printPops "odious " $ filter ( odd . popCount) ([0..] :: [Integer])

View file

@ -0,0 +1,22 @@
import Data.Bifoldable (biList)
import Data.List (partition, unfoldr)
import Data.Tuple (swap)
--------------------- POPULATION COUNT -------------------
popCount :: Int -> Int
popCount = sum . unfoldr go
where
go x
| 0 < x = (Just . swap) (quotRem x 2)
| otherwise = Nothing
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
zipWith
(\k xs -> concat [k, ":\n", show xs, "\n"])
["Population count of powers of 3", "evil", "odious"]
( (popCount . (3 ^) <$> [0 .. 29]) :
biList (partition (even . popCount) [0 .. 59])
)