Skip to content

Instantly share code, notes, and snippets.

@masters3d
Created February 20, 2015 05:52
Show Gist options
  • Save masters3d/e2390bb4cbddeaa43279 to your computer and use it in GitHub Desktop.
Save masters3d/e2390bb4cbddeaa43279 to your computer and use it in GitHub Desktop.
isPrime
//http://stackoverflow.com/questions/5811151/why-do-we-check-upto-the-square-root-of-a-prime-number-to-determine-if-it-is-pri
func isPrime(n: Int) -> Bool {
if n < 2 {
return false // are not primes
}
var limit = Int(sqrt(Float(n)))
if limit < 2 {
return true // 2, 3 are primes
}
for i in 2...limit {
if n % i == 0 {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment