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

54 lines
880 B
Swift

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))")