Created
August 8, 2015 12:33
-
-
Save scotteg/92f263eda20399b2c203 to your computer and use it in GitHub Desktop.
A Swift 2 Int extension that checks if an integer is a prime number.
This file contains 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 { | |
/** | |
`true` if self is a prime number, i.e., can only be divided evenly by 1 and itself. | |
- author: Scott Gardner | |
- seealso: | |
* [Source on GitHub](http://bit.ly/SwiftIntIsPrimeExtension) | |
*/ | |
public var isPrime: Bool { | |
guard self > 1 else { | |
return false | |
} | |
for i in 2..<self { | |
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