The anti-primes
(or highly composite numbers, sequence A002182 in the OEIS)
are the natural numbers with more factors than any smaller than itself.



Task

Generate and show here, the first twenty anti-primes.



Related tasks

   Factors of an integer
   Sieve of Eratosthenes


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