Created
February 20, 2015 05:52
-
-
Save masters3d/e2390bb4cbddeaa43279 to your computer and use it in GitHub Desktop.
isPrime
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
//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