Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
17
Task/Pernicious-numbers/Haskell/pernicious-numbers-1.hs
Normal file
17
Task/Pernicious-numbers/Haskell/pernicious-numbers-1.hs
Normal 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]
|
||||
19
Task/Pernicious-numbers/Haskell/pernicious-numbers-2.hs
Normal file
19
Task/Pernicious-numbers/Haskell/pernicious-numbers-2.hs
Normal 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]
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue