Created
November 23, 2016 13:42
-
-
Save taichunmin/3f7e5c9651659596c5e40b1ec77e7a2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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