Skip to content

Instantly share code, notes, and snippets.

@taichunmin
Created November 23, 2016 13:42
Show Gist options
  • Save taichunmin/3f7e5c9651659596c5e40b1ec77e7a2d to your computer and use it in GitHub Desktop.
Save taichunmin/3f7e5c9651659596c5e40b1ec77e7a2d to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Darwin
var prime = [2, 3, 5, 7]
func isPrime(num: Int) -> Bool {
let primeMax = Int(floor(sqrt(Double(num))))
for i in prime {
if i > primeMax {
break
}
if num % i == 0 {
return false
}
}
return true
}
func buildPrime(max: Int) {
let primeMax = Int(floor(sqrt(Double(max))))
var i = 11, j = 2
while prime[prime.count - 1] <= primeMax {
if isPrime(num: i) {
prime.append(i)
}
i = i + j
j = 6 - j
}
}
buildPrime(max: 1000000)
prime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment