Skip to content

Instantly share code, notes, and snippets.

@oderwat
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oderwat/6007097ebfb0025403c4 to your computer and use it in GitHub Desktop.
Save oderwat/6007097ebfb0025403c4 to your computer and use it in GitHub Desktop.
Test for being prime with a regular expression in Nim (aka Nimrod)
# 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"
@oderwat
Copy link
Author

oderwat commented Feb 13, 2015

It is pretty astonishing how good that works ... and even performs ;-)

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