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,17 @@
module Pernicious
where
isPernicious :: Integer -> Bool
isPernicious num = isPrime $ toInteger $ length $ filter ( == 1 ) $ toBinary num
isPrime :: Integer -> Bool
isPrime number = divisors number == [1, number]
where
divisors :: Integer -> [Integer]
divisors number = [ m | m <- [1 .. number] , number `mod` m == 0 ]
toBinary :: Integer -> [Integer]
toBinary num = reverse $ map ( `mod` 2 ) ( takeWhile ( /= 0 ) $ iterate ( `div` 2 ) num )
solution1 = take 25 $ filter isPernicious [1 ..]
solution2 = filter isPernicious [888888877 .. 888888888]

View file

@ -0,0 +1,19 @@
import Data.Numbers.Primes (isPrime)
import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)
isPernicious :: Int -> Bool
isPernicious = isPrime . popCount
popCount :: Int -> Int
popCount =
sum . unfoldr ((flip bool Nothing . Just . swap . flip quotRem 2) <*> (0 ==))
main :: IO ()
main =
mapM_
print
[ take 25 $ filter isPernicious [1 ..]
, filter isPernicious [888888877 .. 888888888]
]