Skip to content

Instantly share code, notes, and snippets.

@ddribin
Created November 2, 2024 13:55
Show Gist options
  • Save ddribin/fe986a4bbc6e0b30ca2da890373e9f30 to your computer and use it in GitHub Desktop.
Save ddribin/fe986a4bbc6e0b30ca2da890373e9f30 to your computer and use it in GitHub Desktop.
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)
@ddribin
Copy link
Author

ddribin commented Nov 2, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment