TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Regular Expression That Checks If A Number Is Prime

294 pointsby iluxonchikover 8 years ago

18 comments

TimWollaover 8 years ago
It should be noted that this is not possible with regular expressions in the traditional sense (i.e. regular expressions only matching regular languages): <a href="http:&#x2F;&#x2F;math.stackexchange.com&#x2F;a&#x2F;181233&#x2F;21405" rel="nofollow">http:&#x2F;&#x2F;math.stackexchange.com&#x2F;a&#x2F;181233&#x2F;21405</a><p>Because of back references PCRE regular expressions can match non-regular languages as well.
评论 #12459438 未加载
评论 #12458902 未加载
评论 #12458676 未加载
评论 #12460715 未加载
delsartoover 8 years ago
tl;dr if you know a little regex<p><pre><code> - convert your number to a string of that length (1 = 1, 2 = 11, 3 = 111) - handle 0 &#x2F; 1 separately - ^(..+?)\1+$ - trick; the WHOLE thing has to match to return (^$) - first \1 match is &quot;11&quot;, ergo string must be &quot;11&quot;, &quot;1111&quot;, &quot;111111&quot; to match - second \1 match &quot;111&quot;, ergo string must be &quot;111&quot;, &quot;111111&quot;, &quot;111111111&quot; to match - and so on. if you find before length of string, it was not prime. </code></pre> Clever trick. Look forward to being asked it in your next google interview :)
评论 #12459066 未加载
评论 #12458703 未加载
评论 #12459549 未加载
评论 #12458754 未加载
评论 #12460302 未加载
评论 #12458664 未加载
jsrnover 8 years ago
Invented in 1998 by Perl hacker Abigail:<p><a href="http:&#x2F;&#x2F;neilk.net&#x2F;blog&#x2F;2000&#x2F;06&#x2F;01&#x2F;abigails-regex-to-test-for-prime-numbers&#x2F;" rel="nofollow">http:&#x2F;&#x2F;neilk.net&#x2F;blog&#x2F;2000&#x2F;06&#x2F;01&#x2F;abigails-regex-to-test-for-...</a><p>(check out Abigail&#x27;s other JAPHs if you like stuff like this)
评论 #12458940 未加载
fxnover 8 years ago
This regexp is totally brilliant. It solves this problem in an unexpected way, because you think numbers, but the solution thinks strings. Not only that, the paradox, or its beauty, is that it goes to the root of the number abstraction: counting sticks.<p>I explored this technique some years later to solve a bunch of other problems, coprimality, prime factorization, Euler&#x27;s phi, continued fractions, etc. (see <a href="https:&#x2F;&#x2F;github.com&#x2F;fxn&#x2F;math-with-regexps&#x2F;blob&#x2F;master&#x2F;one-liners.sh" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fxn&#x2F;math-with-regexps&#x2F;blob&#x2F;master&#x2F;one-lin...</a>).
jemfinchover 8 years ago
I wish they&#x27;d stop calling these expressions &quot;regular&quot;.
评论 #12458926 未加载
rrauenzaover 8 years ago
&gt; How would we go about that? Well, all we have to do is add ? in front of the +. This will lead us to the &lt;.+?&gt; regex.<p>I was very confused until I realized the author&#x27;s definition of &#x27;in front&#x27; wasn&#x27;t the same as mine...
评论 #12458915 未加载
snickyover 8 years ago
Is it just me or the author of this blog post has a very annoying way of writing? He keeps explaining things and then a couple of lines below he writes something like &quot;remember the discussion above?&quot; or &quot;if you remember correctly ...&quot;. Hell, sure I remember, because I just read about this 10 seconds ago. Do people really have such a short attention span and can&#x27;t remember what was in a previous paragraph? Repeating stuff over and over seems almost like content farming to me.
评论 #12461366 未加载
评论 #12461298 未加载
mwpmaybeover 8 years ago
Very interesting. Thanks for writing this up.<p>This is a bit more Perlish (not that I&#x27;m an authority):<p><pre><code> sub is_prime { (1x$_[0]) !~ &#x2F;^(?:.?|(.{2,}?)\1+)$&#x2F;; } </code></pre> But perhaps less clear to a novice reader. You can also leave off the semicolon.
评论 #12459077 未加载
评论 #12458907 未加载
0xmohitover 8 years ago
I suppose that this has been around for a while:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9039537" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9039537</a><p><a href="http:&#x2F;&#x2F;montreal.pm.org&#x2F;tech&#x2F;neil_kandalgaonkar.shtml" rel="nofollow">http:&#x2F;&#x2F;montreal.pm.org&#x2F;tech&#x2F;neil_kandalgaonkar.shtml</a>
baristaGeekover 8 years ago
Performing a Miller-Rabin primality test or filling a Sieve of Eratosthenes will almost always be the way to go. However, this line of code seems like magic and that&#x27;s kind of impressive.
jimjimjimover 8 years ago
how many problems do we have now?
评论 #12461462 未加载
评论 #12465201 未加载
daenneyover 8 years ago
Why does the example given by the author with L=15 state that the regex matches once it reaches 5, instead of 3?<p>&gt; So first, we’ll be testing the divisibility by 2, then by 3, then by 4 and then by 5, after which we would have a match.
评论 #12461312 未加载
ldom22over 8 years ago
have regular expressions gone too far?
ipozgajover 8 years ago
It&#x27;s basically the Sieve of Eratosthenes[1] algorithm, and it&#x27;s possible to do it with regular expressions because numbers here are represented in unary[2] number system, where number of characters&#x2F;tokens equals the number itself. It&#x27;s a common trick for testing various Turing-machine stuff.<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Sieve_of_Eratosthenes" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Sieve_of_Eratosthenes</a> [2] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Unary_numeral_system" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Unary_numeral_system</a>
评论 #12458984 未加载
YeGoblynQueenneover 8 years ago
Can&#x27;t quite get this to work in vim [1]:<p><pre><code> \(^1\{-,1}$\)\|\(^\(11\+\)\1\+$\) </code></pre> The two parts match what you&#x27;d expect on their own but the OR-ing screws it up: it means the whole regex matches <i>everything</i>.<p>Is this something vim gets right and every other engine wrong, the other way around, or...?<p>_____________<p>[1] I&#x27;m matching 1&#x27;s rather than dots to avoid highlighting every bit of text ever anywhere all the time.<p>Also, that way it&#x27;s so much more pleasing to the eye and easy to read, don&#x27;t you think?
评论 #12460436 未加载
Senjiover 8 years ago
Our hubris will be our undoing.
kgdineshover 8 years ago
how does it fare on time complexity compared to normal tests like say AKS?
评论 #12462259 未加载
评论 #12459548 未加载
caubover 8 years ago
it&#x27;s highly inefficient tho