The Non-Prime Regex is in the cross-section of two of my interests
Roughly 4.5 years ago I shared in this blog my interest for recreational mathematics. Yes, I’m that kind of geek.
I’ve been keeping up with my hobby throughout these years.
I also have confessed my love for regular expressions.
Hence, I was delighted when these two crossed paths in Matt Parker’s fantastic Youtube channel last week on his video How on Earth does ^.?$|^(..+?)\1+$ produce primes?
In it, Parker walks through how the regex
/^.?$|^(..+?)\1+$/
can tell if a number
n
is prime, when the string
"1" * n
(string “1” written n times) is
passed through it.
Illya Gerasymchuk has written a brilliant blog post that really goes in depth to the topic in Demystifying The Regular Expression That Checks If A Number Is Prime so I won’t repeat the steps of the blog post or the video here.
I just wanted to let you know that these two absolutely incredible pieces exist so you can discover the same joys of life that I do.