Skip to content

Instantly share code, notes, and snippets.

@scotteg
Created August 8, 2015 12:33
Show Gist options
  • Save scotteg/92f263eda20399b2c203 to your computer and use it in GitHub Desktop.
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.
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