Last active
August 29, 2015 14:15
-
-
Save oderwat/6007097ebfb0025403c4 to your computer and use it in GitHub Desktop.
Test for being prime with a regular expression in Nim (aka Nimrod)
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
# regular expression to check a number for being prime | |
# | |
# see: http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ | |
import re, strutils | |
let rprime = re"^1?$|^(11+?)\1+$" | |
proc testPrime(n: int): bool = | |
not (repeatChar(n, '1') =~ rprime) | |
for n in 1..10_000: | |
echo n, " is", if not testPrime n: " not" else: "", " prime" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is pretty astonishing how good that works ... and even performs ;-)