Last active
August 29, 2015 14:05
-
-
Save adeubank/939f3292a35d4a6cfa0a to your computer and use it in GitHub Desktop.
Adds an extension to the Int class to tell return whether an integer is prime or not
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
extension Int { | |
func isPrime() -> Bool { | |
if (self == 1) { return false } | |
if (self == 2) { return true } | |
var sqr = Int(sqrt(Float(self))) + 1 | |
for i in 2...sqr { | |
if self % i == 0 { | |
return false | |
} | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment