Jump to content

Anti-primes: Difference between revisions

Add Swift
(Add Swift)
Line 1,747:
[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]
</pre>
 
 
=={{header|Swift}}==
 
<lang 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))")</lang>
 
{{out}}
 
<pre>First 20 anti-primes are [1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]</pre>
 
=={{header|Vala}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.