Jump to content

Unprimeable numbers: Difference between revisions

m
Swift - simplified code
m (Swift - print numbers with commas)
m (Swift - simplified code)
Line 2,480:
 
init(size: Int) {
self.array = Array(repeating: 0, count: (size + 31)/32)
}
func get(index: Int) -> Bool {
let bit = UInt32(1) << (index & 31)
return (self.array[index >> 5] & bit) != 0
}
Line 2,491:
let bit = UInt32(1) << (index & 31)
if value {
self.array[index >> 5] |= bit
} else {
self.array[index >> 5] &= ~bit
}
}
Line 2,499:
 
class PrimeSieve {
varlet composite: BitArray
init(size: Int) {
self.composite = BitArray(size: size/2)
var p = 3
while p * p <= size {
if !self.composite.get(index: p/2 - 1) {
let inc = p * 2
var q = p * p
while q <= size {
self.composite.set(index: q/2 - 1, value: true)
q += inc
}
Line 2,524:
return number == 2
}
return !self.composite.get(index: number/2 - 1)
}
}
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.