RosettaCodeData/Task/Anti-primes/Haskell/anti-primes.hs
2023-07-01 13:44:08 -04:00

28 lines
739 B
Haskell

import Data.List (find, group)
import Data.Maybe (fromJust)
firstPrimeFactor :: Int -> Int
firstPrimeFactor n = head $ filter ((0 ==) . mod n) [2 .. n]
allPrimeFactors :: Int -> [Int]
allPrimeFactors 1 = []
allPrimeFactors n =
let first = firstPrimeFactor n
in first : allPrimeFactors (n `div` first)
factorCount :: Int -> Int
factorCount 1 = 1
factorCount n = product ((succ . length) <$> group (allPrimeFactors n))
divisorCount :: Int -> (Int, Int)
divisorCount = (,) <*> factorCount
hcnNext :: (Int, Int) -> (Int, Int)
hcnNext (num, factors) =
fromJust $ find ((> factors) . snd) (divisorCount <$> [num ..])
hcnSequence :: [Int]
hcnSequence = fst <$> iterate hcnNext (1, 1)
main :: IO ()
main = print $ take 20 hcnSequence