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


extension BinaryInteger {
  @inlinable
  public func countDivisors() -> Int {
    var workingN = self
    var count = 1
 
    while workingN & 1 == 0 {
      workingN >>= 1
 
      count += 1
    }
 
    var d = Self(3)
 
    while d * d <= workingN {
      var (quo, rem) = workingN.quotientAndRemainder(dividingBy: d)
 
      if rem == 0 {
        var dc = 0
 
        while rem == 0 {
          dc += count
          workingN = quo
 
          (quo, rem) = workingN.quotientAndRemainder(dividingBy: d)
        }
 
        count += dc
      }
 
      d += 2
    }
 
    return workingN != 1 ? count * 2 : count
  }
}
 
var antiPrimes = [Int]()
var maxDivs = 0
 
for n in 1... {
  guard antiPrimes.count < 20 else {
    break
  }
 
  let divs = n.countDivisors()
 
  if maxDivs < divs {
    maxDivs = divs
    antiPrimes.append(n)
  }
}
 
print("First 20 anti-primes are \(Array(antiPrimes))")