Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Pernicious-numbers/Swift/pernicious-numbers.swift
Normal file
34
Task/Pernicious-numbers/Swift/pernicious-numbers.swift
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import Foundation
|
||||
|
||||
extension BinaryInteger {
|
||||
@inlinable
|
||||
public var isPrime: Bool {
|
||||
if self == 0 || self == 1 {
|
||||
return false
|
||||
} else if self == 2 {
|
||||
return true
|
||||
}
|
||||
|
||||
let max = Self(ceil((Double(self).squareRoot())))
|
||||
|
||||
for i in stride(from: 2, through: max, by: 1) where self % i == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
public func populationCount(n: Int) -> Int {
|
||||
guard n >= 0 else {
|
||||
return 0
|
||||
}
|
||||
|
||||
return String(n, radix: 2).lazy.filter({ $0 == "1" }).count
|
||||
}
|
||||
|
||||
let first25 = (1...).lazy.filter({ populationCount(n: $0).isPrime }).prefix(25)
|
||||
let rng = (888_888_877...888_888_888).lazy.filter({ populationCount(n: $0).isPrime })
|
||||
|
||||
print("First 25 Pernicious numbers: \(Array(first25))")
|
||||
print("Pernicious numbers between 888_888_877...888_888_888: \(Array(rng))")
|
||||
Loading…
Add table
Add a link
Reference in a new issue