Created
November 2, 2024 13:55
-
-
Save ddribin/fe986a4bbc6e0b30ca2da890373e9f30 to your computer and use it in GitHub Desktop.
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
import Foundation | |
func isPrime(_ n: Int) -> Bool { | |
return !String(repeating: "1", count: n).contains(/^.?$|^(..+?)\1+$/) | |
} | |
func isPrimeAlt(_ n: Int) -> Bool { | |
let s = String(repeating: "1", count: n) | |
return !s.contains(/^.?$|^(..+?)\1+$/) | |
} | |
isPrime(99) | |
isPrime(100) | |
isPrime(101) | |
isPrime(102) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift version of: https://illya.sh/the-codeumentary-blog/regular-expression-check-if-number-is-prime/