• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Noulakaz

Noulakaz

The blog of Avinash, Christina, Anya and Kyan Meetoo.

  • Home
  • About
  • People
    • Christina & Avinash Meetoo
    • Avinash Meetoo
    • Christina Meetoo
    • Anya Meetoo
    • Kyan Meetoo
  • General
    • News
    • Mauritius
    • Politics
    • Education
    • Business
    • Travel
  • Computing
    • Apple
    • Linux
    • LUGM
    • Programming
    • Web
    • Technology
    • Knowledge Seven
  • Entertainment
    • Music
    • Movies
    • Photography
    • Sports

A regular expression to check for prime numbers

18 March 2007 By Avinash Meetoo 170 Comments

Regular expressions are extremely powerful. This is something I read at least once or twice every day while reading articles and blogs on the Web.

While browsing today, I found this page which thoroughly describes the use of the regular expression /^1?$|^(11+?)\1+$/ in Perl to check if a number is prime or not!!!

/^1?$|^(11+?)\1+$/

To be frank, I was skeptical. The regular expression looks like magic! And I wanted to understand it better. I rewrote it in Ruby using irb and I tested it:

Avinash@noulakaz:~$ irb

irb(main):001:0> def is_prime(n)
irb(main):002:1> (“1” * n) !~ /^1?$|^(11+?)\1+$/
irb(main):003:1> end
=> nil

irb(main):004:0> is_prime(10)
=> false

irb(main):005:0> is_prime(11)
=> true

irb(main):006:0> is_prime(12)
=> false

irb(main):007:0> is_prime(13)
=> true

irb(main):008:0> is_prime(99)
=> false

irb(main):009:0> is_prime(100)
=> false

irb(main):010:0> is_prime(101)
=> true

Great! It also works in Ruby! This means that there is no (Perl) magic going on. The regular expression really works. But how? Let’s try to follow the logic behind it.

Is 7 prime?

To know this, the function first generates “1111111” (from “1” * 7) and tries to see if that string does not match /^1?$|^(11+?)\1+$/. If there is no match, then the number is prime.

Notice that the regular expression has two parts (separated with the vertical bar |).

The first part is /^1?$/ is trivial and matches with beginning of line (^), an optional 1 (1?)
and end of line ($) which implies that it matches either the empty string or “1”. This simply indicates that calling that function with n==0 or n==1 will correctly return false (as the “1” * n will match with the first part of the regular expression)

The second part is where the magic occurs…

/^(11+?)\1+$/ matches with beginning of line (^) then by (11+?) then by \1+ and finally by end of line ($). I guess that you know that \1 is a variable which is bound to whatever was matched previously (in our case by (11+?)).

Let’s proceed slowly…

(11+?) does two things

  1. It matches with “1” followed by one or more ones minimally. This means that it matches with “11” initially (notice that if there was no ? (i.e. (11+) was used instead, the whole string would have matched)
  2. The string obtained (“11” initially) is bound to the variable \1.

\1+ then matches with whatever has been matched above (“11” initially) repeated one or more times. If this match succeeds then the number is not prime.

If you are following, you’ll realise that this eliminates all even numbers except 2 (for example, 8 is “11111111” and therefore (11+?) will match with “11” and \1+ will match with “111111”).

As for odd numbers (in our case 7), the (11+?) matches with “11” initially but \1+$ cannot be true (notice the $) as there are 5 remaining ones. The regular expression engine will backtrack and will make (11+?) match “111” and here also \1+$ won’t be true because there will be 4 remaining ones (and \1+$ will only match with a number of ones which is a multiple of 3 followed by end of line) etc. hence “1111111” will not match the regular expression which implies that 7 will be considered as being prime :-)

When I showed this to Christina this morning (true), she told me that this only checked for a number being odd or not. This is also what I felt at the beginning. But it really works. For instance, let’s try to apply it to 9 (which is obviously not even), “1” * 9 should match the regular expression…

“1” * 9 = “111111111”. (11+?) matches “11” initially. \1+$ cannot match because there are 7 remaining ones. Backtracking occurs. (11+?) now matches “111”. And here \1+$ matches the remaining 6 remaining ones! Hence, 9 is not prime.

Easy… and beautiful at the same time ;-)

[Join the discussion on Hacker News]

Filed Under: Education, Linux, Programming, Web

Reader Interactions

Comments

  1. Ketwaroo D. Yaasir says

    19 March 2007 at 18:23

    unfortunately, I am quite allergic to regular expressions… too many bloody flavors to make any sense in the end.

  2. avinash says

    19 March 2007 at 22:48

    I fully understand. I was quite allergic to regexpr too. But little by little I have come to like and use them a lot.

    Let’s see, I use regexpr when working with vi, grep, sed, awk and, now, ruby…

  3. Ketwaroo D. Yaasir says

    19 March 2007 at 23:51

    I’ve been trying to write a bbcode parser for wordpress µ to cure it of kses.php while keeping the entries of the bloggers “safe”. So I have to figure out how to use regex.

    So i guees i too will have to learn to like it.

    It’s like voodoo.

  4. avinash says

    20 March 2007 at 09:42

    And I suppose you have realised that it is a small programming language… so it’s bound to be cool :-)

  5. curiousEngine says

    21 March 2007 at 09:25

    Programming Quiz No. 2

    A serial file named contains a series of values, each of which is how many prime numbers ther are below a particular value. Write a program to read the number of primes between and values where and are requested interactively and compare it to the value obtained from FUNCT(limit) which is defined as limit / LOG(limit).

  6. avinash says

    21 March 2007 at 13:34

    Hi,

    Seems that some of the text went missing… or you were seriously drunk :-)

  7. VagabondoDigitale says

    24 September 2007 at 23:19

    Cool.. thx for your post. I like matematics quiz :P

  8. doh says

    25 September 2007 at 14:53

    nice trick!

    I find that the “backtracking” part was equivalence to finding out if the number is prime by brute-forcing

    e.g. going from 3 to half the number and if any of that is divisible, its not a prime.

    thus the ^(11+?)\1+& trick (anyone got it?)

    i believe there are a lot of fast algorithms out there to use instead of generating 1s and parsing them with a regular expression which is kinda slow.

    however this is truly magic ^^

  9. ksivasenthil says

    29 September 2007 at 07:50

    hey avinash, what is the exact input that goes to the regex constructor. I am not a ruby programmer but my attempt to use this regex in javascript failed. The test always failed for both prime and composite numbers. If you know javascript please help show me a sample to use this in javascript pls

  10. someone says

    3 October 2007 at 17:53

    the trick is to apply the regex to a string with n 1’s, for example, for 5, apply the regex to 11111.

  11. Jay says

    7 October 2007 at 10:55

    A PHP Version:
    !preg_match(‘/^1?$|^(11+?)\1+$/x’, str_repeat(‘1’, $n))

    Example:

    <?php

    $n = 1;
    while ($n < 1000000) {
    if (!preg_match(‘/^1?$|^(11+?)\1+$/x’, str_repeat(‘1’, $n))) {
    echo “$n: Prime\n”;
    }
    $n++;
    }
    ?>

  12. Skymt says

    16 October 2007 at 17:19

    C# version:

    static void Main(string[] args)
    {
    int c;
    if (args.Length == 1 && int.TryParse(args[0], out c))
    Console.WriteLine(!Regex.IsMatch(new String(‘1’, c), @”^1?$|^(11+?)\1+$”));
    }

  13. Binks says

    19 October 2007 at 04:17

    Java Version, nice tip btw, guess I should learn more about regular expressions. Java requires that the \ be \’d out, otherwise it sees it as a smiley face character and problems happen :P.

    public static void main(string[] args){
    if (args.length >= 1)
    {
    int c = Integer.parseInt(args[0]);
    String s = “”;
    while (c > 0) {c–; s+=”1″;}
    System.out.println(!Pattern.matches(“^1?$|^(11+?)\\1+$”, s));
    }}

  14. Venkatesh says

    19 October 2007 at 09:11

    This is just awesome…

  15. Steven says

    30 January 2008 at 02:35

    Hello,

  16. adamsky says

    10 February 2008 at 16:19

    To be honest – regular expressions are “overused”… And are NOT that good. For example in PHP using just common str_replace(); would be in many cases much faster then preg_replace();

    Problem is that people tend to use a lot of “sophisticated” code where it’s not necessary, only because it looks more “professional”. My thought on this is “I don’t care what others think – I just need my code to be fast, clean and as simple as it is possible”.

  17. avinash says

    11 February 2008 at 01:32

    True.

    Sometimes regular expressions are extremely cryptic (like the one above in fact ;-) ) but they are also interesting to understand because they are essentially small programs written in a domain specific language.

  18. Nick says

    19 February 2008 at 04:33

    The magic is in the backtrack…. if it can find a number a=(11+?) that multiplied N times gives our number, it’s obvious that our number can’t be prime (our number = a*N, i.e. not prime)

  19. roscoe says

    26 March 2008 at 02:19

    How many numbers does this count up to? whats the largest prime that can be found in *real* time?

    I’m interested so mail me please.

  20. avinash says

    26 March 2008 at 23:24

    Hi Roscoe,

    I’ve not done any performance test but I guess that if you use a compiled language like C with a good Regex implementation, then, maybe, you can generate prime numbers real fast.

    On the other hand, if you really want to generate primes then have a look at The Sieve of Eratosthene.

  21. Paul says

    2 April 2008 at 23:38

    Avinash. Super, I’ve been looking around for a Regex similar to this for a while. I’ve modified it to suit.

  22. avinash says

    3 April 2008 at 19:32

    Hi Paul,

    Great to know that you find this regular expression useful. I’d like to point out that I’m not the original author though ;-)

  23. rasmus says

    27 April 2008 at 03:22

    if primenumber mod 2 is 0 then
    isprime = true

    wouldnt that be easier and faster? ^^

  24. rasmus says

    27 April 2008 at 03:30

    sorry, its getting late here heh

    if primenumber mod 2 != 0 then
    isprime = true

  25. avinash says

    27 April 2008 at 07:20

    Hi Rasmus,

    A prime number is not simply odd. Check this.

  26. Celina says

    30 June 2008 at 15:44

    I have never been able to understand regular expressions, what is the logic behind them?

  27. avinash says

    1 July 2008 at 06:54

    Hi Celina,

    Regular expressions are not trivial. But they are very powerful. If you want to know more about them, this is a good start.

    A lot of people also recommend this book.

  28. Anonymous says

    10 July 2008 at 03:56

    While it’s neat, it’s not a great idea to use for big numbers since it uses O(N^2) time and O(N) memory where even a trivial solution uses O(sqrt(N)) time and O(1) memory.

  29. avinash says

    10 July 2008 at 07:56

    True :-)

    I wonder if someone has actually done some benchmarking with large numbers?

  30. Anonymous says

    10 July 2008 at 17:53

    This regular expression does not use O(1) and O(N^2), its actually exponential in the size of the input. If it unpacks the number 7 (32 bit) to 7 1’s, thats 7*(32). So a 32 bit number 2147483648 would take up half a gig of memory.

  31. Kadhirvel says

    29 July 2008 at 21:06

    Please follow the link below for fact about primes

    http://kadiprimality.blogspot.com/

  32. nidhi says

    9 January 2009 at 11:59

    Pls give me regex to match filename that doesn’t start with a number and have html extension.

  33. avinash says

    9 January 2009 at 16:05

    Maybe something like “^[^0-9].*html$”

    Notice that the two ^ have very different meanings. The first one is an anchor which represents the beginning of the line. The second one is the “inverse” operator i.e. [^0-9] matches with any character excepts a digit.

  34. Skymt says

    9 January 2009 at 17:02

    You could also try this: “^\D\w+.html”

    ((^)Start of line – (\D)Not a digit – (\w+)several letters – (.html)end with .html)

    Works with the .NET flavor of regex…

  35. rentis3 says

    31 January 2009 at 19:41

    for calculations with big numbers, try F#

  36. erwin says

    31 March 2009 at 21:35

    try this: 999999 on the above php regexp it says prime….
    it isn’t ….

  37. avinash says

    1 April 2009 at 11:11

    So it seems that there is a bug lurking somewhere…

  38. cetin says

    12 April 2009 at 17:51

    I don’t think this is formally a regular expression, because of “?” character. The processing of a real regular expression never requires backtracking (i.e. never requires reading the input multiple times) and hence they are very fast once you generate the deterministic finite automata (DFA) for the given regular expression.

    Programming languages usually provide extensions to regular expressions (like ? in this case) for convenience to the programmer. There are only 3 operators in formal regular languages, which are star (“*”), union (“|”) and concatenation. All other operators used in a regular expressions must be representable by using these 3, for example you can represent 0+ as 00* and so on. The operator “?” used in the above expression is something totally different and which in fact violates the rule that machines that process a regular expression can do so with finite amount of memory. Hence this expression is actually not regular and the language generated by it is not a regular language, mathematically.

  39. avinash says

    12 April 2009 at 20:35

    Mathematically, you may be right (but I am not so sure.) But, from a programming point of view, regular expressions have always allowed backtracking…

  40. Fred says

    10 July 2009 at 21:25

    php may reject large numbers silently due to recursion depth limit (which I think is 32767 levels), that’s why 999999 fails.

  41. avinash says

    12 July 2009 at 22:48

    Thanks for pointing this out…

  42. Patrick Atambo says

    16 July 2009 at 23:25

    WOW, pretty cool! I have to revisit regular expressions it seems. :)

  43. akash says

    17 September 2009 at 05:23

    a language such that a string is made of binary alphabet( of 0s and 1s) and the number of zeros in the string is a 3 digit prime number…….is this language regular…??
    can u pls help me out in this ……its a ques appeared in GATE entrance

  44. perler says

    3 November 2009 at 16:14

    this was good shit. thanks!

  45. Pras Sarkar says

    18 November 2009 at 22:23

    This is really intriguing, but I’m unclear about your explanation about how it works (it definitely works). For example, try the number 49 (not prime) – 1111111111111111111111111111111111111111111111111 – neither (11)+ matches nor (111)+ matches, so you would think the regex would classify 49 as prime (it does not). So how does it really work?

  46. Makovec says

    4 February 2010 at 20:24

    I would rather see pattern like this: “^\d*[13579]$” in my scripts.

    It’s clear, easy and I think it’s also faster then the one described. If you want negative numbers it’s easy to change.

    Anyway it’s nice work and idea!

    David

  47. Makovec says

    4 February 2010 at 20:29

    Oh, hell – my bloody English. Forgot my previous note :( Shit

  48. Nithin Bekal says

    15 May 2010 at 10:20

    Wow, that’s impressive. A colleague of mine showed me this regular expression, and I couldn’t really believe it was possible. I’ve never really learned regular expressions, but this has definitely encouraged me to start learning.

  49. avinash says

    15 May 2010 at 23:29

    @Nithin

    Yeah. They are cool. Personally, I practically never use the very complex ones (like the one above) but I routinely use the more usual ones especially when I’m using vi or TextMate on my MacBook.

  50. ap2cu says

    17 May 2010 at 20:24

    Nice!!
    I implemented it on my website: http://ap2cu.com/tools/prime
    This is working great
    You can also check my REGEX online tool: http://ap2cu.com/tools/regex

    Thanks for this post!

  51. Sean Walker says

    5 July 2010 at 02:12

    @Pras Sarker: For your example 49, it works because it matches (1111111+) and \1+ (7×7). Basically it starts by matching 11+ (2 times something), then 111+ (3 times something) then 1111+ (4 times something). It starts at 2 and looks at each succeeding number to see if it is a factor (by matching it and then matching multiples of it). But like everyone said this method is very very slow, the bigger the number you are checking the worse the performance, by a LOT.

    @Makovec: You and several other people don’t seem to understand what a prime number is. A prime number is not an odd number. The number 21 would match your simplified regexp because it is odd (ends in 1, matching your expression) but it should FAIL the prime number check because it has factors 3 and 7 (3×7=21). You can’t simply look for odd numbers.

    This is probably the simplest way to check for prime numbers within a single regexp for sure, but because there are so many non-regexp ways to do it (which are MUCH more efficient), this is just a novelty and not actually useful for any real world application.

    Still WAY cool though!! :)

  52. avinash says

    5 July 2010 at 06:19

    Exactly :-)

  53. Paulo Geyer says

    5 July 2010 at 08:00

    Did you notice that you could do this also:
    def is_prime(n)
    (100-2)%2.0 != 0
    end

    it’s just amazing, and really easy

  54. Sachin says

    5 July 2010 at 09:22

    awesome..cool..another feather to my knowledge…thanks for sharing..

  55. Paulo Geyer says

    5 July 2010 at 09:28

    wait, my code doesn’t work.
    This (11+?)\1+$ is more complex than i thought

  56. avinash says

    5 July 2010 at 10:25

    You’re right.

    The regular expression does not test for an odd number. Instead, it tests all divisors which is way more complex and, dare I say, more beautiful :-)

  57. Vlado says

    5 July 2010 at 11:37

    Interesting expression, but I think the real power of regular expressions is that they are effective, because they are implemented as finite state automates (FSA). Although this one cannot be implemented like such, because of this backtracking. In theory FSA and regular expressions CANNOT check for prime numbers (check Pumping Lemma ;) ). I think such implementations of RE are wrong, because they obviously don’t guarantee you the O(n) complexity of FSA and can become very inefficient :(

  58. Craig says

    5 July 2010 at 12:53

    @Paulo:
    Assuming you intend to return the result of that equation, you are effectively returning false all the time, as you simply take the modulus of 98 with respect to 2.0.

    I’m not really sure where you’re headed with that but it looks like you too have confused primes with odd numbers.

  59. Andi Kalsch says

    5 July 2010 at 16:42

    JavaScript one:

    var n = 5; !/^1?$|^(11+?)\1+$/.test((function(n){s=”;while(n–)s+=’1′;return s;})(n))

  60. bogdan says

    5 July 2010 at 17:29

    For the love of God … stop calling it a regular expression because it is NOT a regular expression. Awesome as it may be, this is just a recursive function written using (mostly) only symbols that are often associated with regular expressions.

    It REALLY is a proven fact that no regular expression can be devised that accepts all and only prime numbers.

    I truly love the idea though.

  61. Paulo Geyer says

    5 July 2010 at 20:23

    @Craig

    after i wrote the code, I’ve noticed that it just tests for odd numbers. I should pay more attention before posting.

    Actually, I didn’t understand how (11+?)\1+$ really works, there is any math paper about this approach to test the primality of number? I’ve never seen that before

  62. avinash says

    6 July 2010 at 06:07

    @Paula The key is to understand that the matching is being done with a string of ones of lenght N, the number being tested for primality.

  63. brandon says

    8 July 2010 at 21:31

    Doesn’t work? http://rubular.com/r/PGckdeL1Gq

  64. avinash says

    8 July 2010 at 22:17

    The RE should be matched with a string of ones of length N, the number being tested.

  65. brandon says

    8 July 2010 at 22:27

    ahhh, I wan’t paying attention. That’s what I get for skimming over your blog post…

    Great work!

  66. Colin says

    20 July 2010 at 18:55

    Regular expressions aren’t powerful. http://en.wikipedia.org/wiki/Chomsky_hierarchy#The_hierarchy In fact they’re strictly less powerful than most languages which are turing-complete.

  67. Utkarsh says

    21 July 2010 at 08:26

    :|

    Is there anything regex can’t do?

    :|

  68. Chmike says

    21 July 2010 at 09:50

    I had a hard time understanding the algorithm used. Your article doesn’t make it clear.

    The algorithm detects if the input string of 1 can be split in 2 or more shorter strings of same length and with at least two 1.

    It will obviously work, but it’s just a curiosity. Not very efficient.

  69. Nick says

    21 July 2010 at 10:15

    You might also find amuzing my rather silly article on calculating fibonacci number with regexes:

    http://nick.zoic.org/2010/06/01/fibonaci-regex-perversity/

    Bogdan @72 is right, it’s not really a “Regular Expression” as such, but I think it is still appropriate to call it a Regex because most regex engines support this kind of silliness :-).

  70. thomas says

    23 July 2010 at 05:35

    This is a terrible idea. Just because you can do it, it does not mean you should. I am sure I can hammer down a nail with my $2000 guitar :)

  71. avinash says

    23 July 2010 at 07:17

    Yeah. Regular expressions are not very efficient. But you have to agree that they have a great “wow” effect :-)

  72. venki says

    23 July 2010 at 10:22

    fan++ to this blog. :) Awesome post, i’m recently into extensive usage of reg-ex and this works like a charm :) thank you.

  73. Deepti says

    23 July 2010 at 12:39

    I tried the Java version(given by Binks) –
    public static void main(string[] args){
    if (args.length >= 1)
    {
    int c = Integer.parseInt(args[0]);
    String s = “”;
    while (c > 0) {c–; s+=”1″;}
    System.out.println(!Pattern.matches(“^1?$|^(11+?)\\1+$”, s));
    }}

    Doesnt work for me. Can anyone help?

  74. Braden says

    23 July 2010 at 19:50

    @Ketwaroo

    Just use HTMLPurifier.

  75. Joe says

    23 July 2010 at 21:01

    A very silly, useless algorithm, beautifully expressed. :-p

  76. Loki says

    24 July 2010 at 00:05

    Try this instead.

    public static void main(String[] args){

    String[] nums = {“0”, “1”, “3”};

    if (nums.length >= 1)
    {
    int c = Integer.parseInt(nums[2]);
    String s = “”;
    while (c > 0)
    {
    c –;
    s += “1”;
    }
    System.out.println(!Pattern.matches(“^1?$|^(11+?)\\1+$”, s));
    }
    }

  77. Mauro says

    27 July 2010 at 10:03

    Hi,
    I think there’s a lot of confusion about what a Regular Expression is (and what it can do)
    I don’t know where to start.

    The “magic pattern” shown above is not a Regular Expression, but a “regex”, just an input string that a “specific” pattern-patching-engine can process.

    “Regular expressions describe regular languages in formal language theory. They have thus the same expressive power as regular grammars. Regular expressions consist of constants and operators that denote sets of strings and operations over these sets, respectively.” – Wikipedia

    “Many features found in modern regular expression libraries provide an expressive power that far exceeds the regular languages. For example, many implementations allow grouping subexpressions with parentheses and recalling the value they match in the same expression (backreferences). This means that a pattern can match strings of repeated words like “papa” or “WikiWiki”, called squares in formal language theory. The pattern for these strings is (.*)\1.
    …
    Many tools, libraries, and engines that provide such constructions still use the term regular expression for their patterns. This has led to a nomenclature where the term regular expression has different meanings in formal language theory and pattern matching. For this reason, some people have taken to using the term regex or simply pattern to describe the latter” ” – Wikipedia

    This is not just a linguistic problem.

    Looks at algorithm to evaluate “regexes”:

    “The third algorithm is to match the pattern against the input string by backtracking. This algorithm is commonly called NFA, but this terminology can be confusing. Its running time can be exponential, which simple implementations exhibit when matching against expressions like (a|aa)*b that contain both alternation and unbounded quantification and force the algorithm to consider an exponentially increasing number of sub-cases. This behavior can cause a security problem called Regular expression Denial of Service – ReDoS, which might be used by hackers who want to attack a regular expression engine.” – Wikipedia

    And our “magic pattern” is (11+?)\1+

    Now. because an input string, to be declared as “not matching”, must not match ALL of possible paths, the poor engine must evaluate all of them. Here is the problem.

    Imagine now to insert a very low number (around a million).
    Poor engine should evaluate a string long a million 1s!! Oh my god…

    So please, use this funny regex just for study, or to understand how a backtracking engine works, but again, please, think of your children, don’t try this at home.

  78. avinash says

    27 July 2010 at 10:10

    You’re 100% right.

    This regular expression, or more precisely, this regex is pathetically inefficient yet so beautiful in a geekish sort of way :-)

  79. gphilip says

    27 July 2010 at 23:35

    WOW, this is awesome!

  80. Willian says

    30 July 2010 at 18:22

    Amazing, but not fast at all with big numbers =p
    Imagine you have to verify a larger number, like 4294967293 =D

  81. Sumeet Chawla says

    30 July 2010 at 20:42

    Even I am pretty interested in regular expressions. I learned them by using in VI and linux but now I find its use a lot while coding in php.

  82. Adar Wesley says

    31 July 2010 at 21:34

    Hi,

    I didn’t go through all the comments above, but I don’t think anyone actually explained why the regexp works, so I’ll give it a try.

    The secret is in the representation of the number. The number is represented as a sequence of ones (‘1’) as long as the number. What the regexp does is try to see if this
    sequence of ones “can be broken down into 2 or more equal parts”.
    Wait a second, that’s exactly like saying the number is divisible. The dividers are the length of the substring matched by (11+?) and the number of times it occurs in the original string.

    By the way, I agree that this is extremely inefficient.
    I also agree with @avinash that it’s “beautiful in a geekish sort of way”.

  83. Sterling (Chip) Camden says

    31 July 2010 at 21:57

    I ran some benchmarks: http://www.chipsquips.com/?p=2418&cpage=1#comment-265857

    It’s far too inefficient for practical use, but it’s mathematically brilliant.

  84. Svetlana Wiczer says

    1 September 2010 at 22:12

    Awesome. I love regex. Your regex makes perfect sense from the start. Have you ever tried to draw a table 7-cells wide and fill it row by row with numbers 1-2-3-4 (to infinity – lol) then circle primes? You’ll see beautiful diagonal patterns.

  85. avinash says

    1 September 2010 at 22:35

    I’ll do that tomorrow, Svetlana :-)

  86. Mike Bethany says

    8 October 2010 at 09:04

    Very cool trick and probably useful for small prime number tasks but of course it’s just not feasible for any really heavy math. Neat trick though.

  87. hanicker says

    13 October 2010 at 06:04

    Everybody loves primes!

  88. Prestaul says

    15 December 2010 at 21:48

    Better JS version:

    function isPrime(val) {
    return !/^1?$|^(11+?)\1+$/.test((new Array(val + 1)).join('1'));
    }

  89. kiran pawar says

    15 May 2012 at 15:53

    # script to check wheather no. is ‘composite no’.
    # if nothing print’s then given no. is ‘prime’.

    my $no = $ARGV[0];

    for(my $i=2; $i<=$no; $i++){

    my $result=($no/$i);

    if($result=~/^[0-9]+$/ && $result=~/[^1]/){

    print"$result \* $i\n";

    print"$no is composite number";

    exit;
    }

    }

  90. Avinash Meetoo says

    15 May 2012 at 18:10

    Thanks for sharing!

  91. Niranjan says

    4 February 2013 at 01:23

    I think this is cool. But it’s not magic. What the regex matcher is doing is that it’s trying to split a unary number into equal parts. 11+? is like a dividend and \1+ is repeater of the dividend. If no dividend exists for a unary representation of a number then it is prime. So it will always work, although it’s not different from checking whether given number is divisible first by 2 then by 3 and then by 4 and so on.

  92. Asen Bozhilov says

    24 April 2013 at 19:37

    Almost agree with @cetin, but he misses some points. He is right that the regular language is some language for whom we are able to construct the DFA. Most of today’s regular expression flavours use NFA, but that doesn’t make the irregular since for every NFA we can construct the corresponding DFA. Regarding the backtracking, this is naturally behavior in NFA, since we have to evaluate all the paths of the NFA.

    I am a little bit frustrated that only one person mention UVW theorem. Indeed you can use the Pumping lemma to prove that is irregular or regular language: http://en.wikipedia.org/wiki/Pumping_lemma_for_regular_languages

  93. Avinash Meetoo says

    24 April 2013 at 22:15

    Thanks Asen for your comment. I don’t understand everything you write but I feel you are right :-)

  94. Stephan Eriksen says

    29 April 2013 at 23:46

    That is so sexy. OMG.

  95. Ken Ficara says

    1 July 2013 at 00:23

    I love this, but it’s not a regular expression (not formally, and not even by Perl standards). The regex backtracks and counts, which is not possible for a formal regular expression, and wouldn’t work at all without the implicit loop that turns the number into a string of 1’s.

    But a thing of beauty nontheless.

    http://blog.kenficara.com/2013/06/30/irregular-language-and-regular-expressions/

  96. Nadim says

    11 July 2013 at 22:30

    Since I’m a web developer (Front-end and Back-end), I always read articles concerning my field. So I was reading about the “Adventures in Javascript development” by Rebecca Murphey. Her blog was powered by Octopress, a blogging framework for hackers. I went to find out more about Octopress and while going through it’s documentation and plugins, I found the actor in this Noulakaz article, that is the regex, being mentioned + a link to the source article (this one) added in the comments.

    Here’s the plugin page: http://octopress.org/docs/plugins/backtick-codeblock/

    Congrats Avinash !

  97. Avinash Meetoo says

    12 July 2013 at 06:00

    Thanks for noticing. This post on regular expressions is definitely the most popular post of this blog. I’m happy I’ve managed to write something people find useful. I’m not the one who found the regular expression though :-)

  98. Tom Lord says

    13 February 2015 at 14:34

    I’m currently working on a ruby gem to generate *examples* of given regular expression. For example:

    /this|is|awesome/.examples # => [“this”, “is”, “awesome”]

    So, I tried it out on this “prime number validator” regex:

    /1?|(11+?)\1+/.examples
    #=> [“”, “1”, “1111”, “111111”, “11111111”]
    /1?|(11+?)\1+/.examples(max_repeater_variance: 50, max_group_results: 100).uniq.sort.map(&:length).first(10)
    #=> [0, 1, 4, 6, 8, 9, 10, 12, 14, 15]

    require ‘prime’
    Array(1..100) – /1?|(11+?)\1+/.examples(max_repeater_variance: 50, max_group_results: 3000).uniq.sort.map(&:length) == Prime.first(25)
    #=> true

    Horribly inefficient, but pretty cool! Here’s my gem if case anyone’s interested: https://github.com/tom-lord/regexp-examples

  99. Avinash Meetoo says

    13 February 2015 at 20:56

    Thanks Tom for sharing.

  100. Robert Clausecker says

    29 January 2016 at 21:24

    This thing does not describe a regular language. I refuse to call it a regular expression. Backreferences are not part or ordinary regular expressions.

  101. Jaap Hollander says

    7 June 2016 at 16:09

    The language of strings that have prime length over the alphabet {1} is not regular. This follows directly from the pumping lemma.

    This means, that a “regex” that find primes relies on extensions and we cannot expect it to work in general.

  102. David Bruchmann says

    19 May 2017 at 16:12

    Just want to mention that pseudo-primes are not found, so even the regex is damned slow with larger numbers it seems being reliable.
    Nevertheless max recursion-levels make it unreliable in PHP for larger numbers, see the comments above.

  103. David Bruchmann says

    19 May 2017 at 16:21

    Here is an array of pseudo-primes for those who like to test it:

    $pseudoprimes = array(9,15,21,25,27,33,35,39,45,49,51,55,57,63,65,69,75,77,81,85,87,91,93,95,99,105,121,133,185,217,273,301,305,323,325,341,377,481,511,561,585,645,657,671,703,705,781,793,817,949,989,1001,1105,1111,1159,1233,1261,1281,1333,1387,1417,1541,1661,1729,1825,1829,1891,1905,2047,2101,2257,2353,2465,2501,2665,2701,2737,2821,2981,3201,3239,3277,3281,3367,3421,3565,3589,3641,3745,3751,3827,3913,4033,4097,4181,4187,4369,4371,4525,4577,4641,4681,4921,4961,5041,5185,5459,5461,5551,5611,5713,5777,6305,6533,6541,6601,6697,6721,7161,7381,7449,7777,7813,7957,8113,8149,8321,8365,8401,8481,8911,9071,9179,9265,9709,10001,10225,10261,10585,10745,10877,11041,11111,11137,11419,11521,11663,12025,12209,12403,12545,12801,13021,13201,13333,13665,13919,13981,14089,14701,14839,14981,15251,15457,15709,15751,15841,16109,16211,16297,16531,16705,17119,17329,17711,18361,18407,18705,18721,18971,19043,19345,20017,20197,20417,20425,21049,21361,22049,22499,23407,23521,23653,24211,24465,24569,24661,25199,25351,25761,25829,25877,26069,27323,27971,28009,29281,29341,29539,29681,29857,29891,30121,30673,30739,30857,30889,31021,31621,31631,31697,32759,33153,34441,34561,34943,34945,35207,35425,35785,36301,38081,39059,39203,39305,39331,39493,39689,40309,40501,41041,42799,43621,44099,44173,44287,44801,46657,46979,47197,47641,47879,48133,49141,49241,49321,50183,50881,51841,51983,52633,52701,53663,53971,54705,55969,56033,58519,58825,60377,63139,63973,64079,64681,67861,67921,68251,72041,72389,73919,74593,75077,75241,75361,76627,78937,79381,80189,90061,90241,96049,97439,97921,98173,100065,100127,100651,102311,105281,113573,115639,118441,125249,130139,137549,137801,146611,153931,155819,158399,161027,162133,162401,163081,172081,176399,176471,186961,189419,192509,197209,197801,218321,219781,224369,230691,231703,243629,249331,252601,253259,254321,257761,268349,268801,271441,272611,288919,302101,313499,324899,370229,399001,429479,430127,449065,459191,473891,480689,488881,530881,600059,621781,632249,635627,656601,670033,741751,838201,851927,904631,997633,1024651,1033997,1050985,1106327,1149851,1256293,1317121,1388903,1392169,1533601,1615681,1653601,1690501,1697183,1773289,1857241,2113921,2263127,2433601,2435423,2455921,2662277,2704801,3057601,3175883,3218801,3224065,3338221,3399527,3452147,3581761,3664585,3774377,3828001,3900797,3967201,4109363,4226777,4403027,4463641,4828277,4870847,4903921,5481451,7405201,7640137,7678321,8539786,9264097,9439201,9532033,9582145,10237921,12813274,16532714,17340938,24658561,27422714,27664033,31150351,33940178,46672291,64132426,89733106,95173786,102690901,109437751,130944133,139952671,178482151,187473826,196075949,203211098,214038533,234735586,284301751,353686906,383425351,395044651,407282851,417027451,498706651,517697641,545670533,582799951,612816751,799171066,801123451,855073301,903136901,919831058,970355431,1091327579,1133818561,1188287794,1235188597,1389675541,1502682721,1955272906,2059739221,2166139898,2304156469,2309861746,2864860298,2976407809,3273820903,3841324339,3871638242,3924969689,4982970241,5130186571,5242624003,5313594466,5867301826,6335800411,7045248121,7279379941,7825642579);
    On request I can send more details about it.

  104. France fermeture says

    25 June 2018 at 01:08

    Article très instructif… :)

  105. Gerard says

    19 October 2018 at 12:28

    I get pleasure from, lead to I discovered just what I used to
    be taking a look for. You have ended my four day long hunt!

    God Bless you man. Have a nice day. Bye

  106. aiden says

    15 April 2019 at 18:34

    JavaScript version in 1 line
    var isPrime=(val)=>!/^1?$|^(11+?)\1+$/.test(‘1’.repeat(val));
    isPrime(19)==true

  107. aarogyam 1.3 says

    26 July 2021 at 12:10

    Very nice patterns collection, I really like it, Very creative.Thanks a lot for sharing !!

  108. Jesse Burström says

    5 March 2022 at 12:23

    Fortunately all research is to no pattern matching for primes they are truly random so only quantuum computers can upheaval cryptography.

    This is though the opposite of that: prime numbers are truly magical expressions!

  109. seniormars says

    17 May 2022 at 22:00

    Below is the rust version

    “`rust
    // regular regex crate does not support backtracking
    use fancy_regex::Regex;

    /// Tests if a positive integer is a prime or not
    ///
    /// # Notes
    /// Extremely inefficient
    ///
    /// # Panics
    ///
    /// Panics if the regex cannot be created
    /// Panics if number is extremely large or much greater than recusion limit
    pub fn is_prime(number: usize) -> Result {
    let re = Regex::new(r”^1?$|^(11+?)\1+$”)?;
    let possible_prime_match = “1”.repeat(number);

    match re.is_match(&possible_prime_match) {
    Ok(is_prime_bool) => Ok(!is_prime_bool),
    Err(err) => Err(err),
    }
    }

    #[cfg(test)]
    mod tests {
    use crate::is_prime;

    #[test]
    fn blog_examples() {
    assert!(!is_prime(10).unwrap(), “10 is not a prime”);
    assert!(is_prime(11).unwrap());
    assert!(!is_prime(12).unwrap(), “12 is not a prime”);
    assert!(is_prime(13).unwrap());
    assert!(!is_prime(99).unwrap(), “99 is not a prime”);
    assert!(!is_prime(100).unwrap(), “100 is not a prime”);
    assert!(is_prime(101).unwrap());
    }

    #[test]
    fn psuedoprimes() {
    let psuedoprimes = vec![
    9, 15, 21, 25, 27, 33, 35, 39, 45, 49, 51, 55, 57, 63, 65, 69,
    ];

    assert_eq!(
    psuedoprimes
    .iter()
    .map(|x| is_prime(*x).unwrap())
    .collect::<Vec>(),
    vec![false; psuedoprimes.len()]
    )
    }

    #[test]
    fn large_prime() {
    // shows how slow this matching process is
    let large_prime: usize = 3010349;
    let large_non_prime: usize = 3010350;
    assert!(is_prime(large_prime).is_err(), “Stack Overflow”);
    assert!(is_prime(large_non_prime).is_err(), “Stack Overflow”)
    }
    }
    “`

  110. Avinash Meetoo says

    18 May 2022 at 07:26

    Thanks seniormars. I always wanted to learn a bit of Rust (and Go to be honest). Why do you like Rust?

  111. seniormars says

    19 May 2022 at 07:09

    I like Rust because it gives me absolute control when I need it, but at the same time, the language is so high level that I can rapidly create the software I need. In other words, rust is a language that allows me to focus on programming while maintaining the speed you would expect from C and C++ — that’s powerful. Another reason I particularly love rust is the excellent compiler and type system rust has. While the compiler can be seen as a strict nuisance, I believe a better way to see it is a teacher that tries to guide you to write the best code. Additionally, I like that everyone uses cargo — the rust package manager to lint, publish code, run tests, and create documentation. This way, you won’t ever have to deal with the issues we see in make/CMake. Finally, what captures my heart is that Rust features many functional programming aspects. From abstract data structures, lazy evaluation, to upcoming features such as higher-rank traits, rust captures the best part of multiple programming paradigms. At one point, you eventually want to use rust for everything if you can, and that is why it is an extremely loved language — because the community believes in it.

    I’m sure that once you start using rust, you will realize the power of a language based on modern programming language theory.

  112. Avinash Meetoo says

    19 May 2022 at 08:42

    Thanks for your very insightful observations. I did try Rust some time ago and I liked it. I’ve a CS background myself and love type theory (I spent a lot of time with Haskell in the past) and functional programming (for me, Scheme is perfection). I’ll get into Rust again. Thanks.

  113. P V says

    17 July 2023 at 15:25

    In vim scripts, the expression ‘\v^1?$|^(11{-1,})\1+$’ works.

Trackbacks

  1. This must be shared: /^1?$|^(11+?)1+$/ to check for prime numbers at Philipp Meier’s weblog says:
    27 February 2008 at 13:55

    […] More details on Avinash Meetoo’s Blog. […]

  2. rul3z » Blog Archive » Calculating a prime number with a shell script says:
    13 September 2008 at 05:50

    […] The script seems to be working, but is not very efficient. While writing this script I found a regular expression that does the same thing, only a couple of hundred times […]

  3. Blogaholic » RegEx zur Primzahlsuche says:
    24 December 2008 at 21:03

    […] Quelle: A regular expression to check for prime numbersTags: merken […]

  4. links for 2009-01-10 | The Tech Guy says:
    11 January 2009 at 03:01

    […] Avinash Meetoo: Blog » A regular expression to check for prime numbers (tags: regexp prime number) Dans la catégorie Del.icio.us « links for 2009-01-09 […]

  5. Primzahlen mit RegEx bestimmen (the nerdy way) | Webmaster, Security und Technik Blog says:
    11 January 2009 at 18:21

    […] habe ich bei Noulakaz einen sehr verwirrenden Weg gefunden eine Zahl auf eine Primzahl hin zu prüfen. Also ob zum […]

  6. is it normal to be kicked out of irb? | keyongtech says:
    18 January 2009 at 21:42

    […] is_prime?(1000000000) [FATAL] failed to allocate memory $ p.s.: is_prime? method taken from https://www.noulakaz.net/2007/…prime-numbers/ — Posted via […]

  7. Prime Number Regex! » Code Musings and Such says:
    19 January 2009 at 10:57

    […] an nice explanation of how it works. […]

  8. CanoeLabs » Regexp says:
    26 February 2009 at 19:08

    […] source. […]

  9. Using the regular expression backtracking engine for prime number magic | Barklund.org says:
    10 August 2009 at 19:33

    […] just saw the most impressive, intelligent use of the regular expression backtracking engine and simply have to re-post it in order to give my own decryption of the infinitely simple […]

  10. Walking Randomly » Finding prime numbers using voodoo regular expressions says:
    27 August 2009 at 14:19

    […] Update (27th August 2009): Someone pointed me to an alternative explanation here. […]

  11. Weekend miscellany — The Endeavour says:
    21 November 2009 at 16:01

    […] A regular expression to check for prime numbers […]

  12. A regular expression to check for prime numbers » News, Hacker, View, Comments » Tjoozey Labs Development - tjoozey.com says:
    5 July 2010 at 03:53

    […] full post on Hacker News If you enjoyed this article, please consider sharing it! Tagged with: check […]

  13. How does this regex find primes? | The Largest Forum Archive says:
    21 July 2010 at 07:39

    […] This page claims that this regex discovers non-prime numbers (and by counter-example: primes): […]

  14. Expressão regular para determinar se um número é Primo « kad says:
    27 July 2010 at 16:11

    […] A regular expression to check for prime numbers | Avinash Meetoo: Blog Divulgue Posted Tuesday, July 27th, 2010 (6 seconds ago) under Informática, Matemática. Tags: expressão regular, números primos, php […]

  15. SavaitÄ—s skaitiniai #1 « Tyliu says:
    29 July 2010 at 20:24

    […] Rasti pirminis skaičius padÄ—s “regular expressions”: A regular expression to check for prime numbers […]

  16. Chipping the web: July 30th -- Chip's Quips says:
    31 July 2010 at 00:15

    […] A regular expression to check for prime numbers | Avinash Meetoo: BlogIt is beautiful…Tags: perl ruby regex primenumbers Tags: atom, consulting, del.icio.us, feeds, mutt, newspipe, opml, perl, poll, primenumbers, regex, rss, ruby, techrepublic […]

  17. A regular expression to check for prime numbers « Yet Another Technology Blog says:
    31 July 2010 at 05:23

    […] A regular expression to check for prime numbers By Rafael Aroca https://www.noulakaz.net/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ […]

  18. The Prime That Wasn’t » Andrei Zmievski says:
    3 August 2010 at 21:29

    […] Sean, I found out that one can use them to determine whether a given integer is a prime number. The original articles showed the following regular […]

  19. Biweekly Links – 08-06-2010 « God, Your Book Is Great !! says:
    7 August 2010 at 00:32

    […] A regular expression to check for prime numbers This post got lot of attention recently. The claim to fame is a clever (but inefficient) regular […]

  20. ALG System.out.println("Hello World!"); - 9lives - Games Forum says:
    10 August 2010 at 00:51

    […] de mensen die het nog niet gezien hebben, dit vond ik wel leuk. Werkt wel niet perfect in PHP voor grote getallen door een beperking op […]

  21. Regex wtf « The Scrambled Bit says:
    29 August 2010 at 20:37

    […] Via Coding Horror È™i noulakaz.net […]

  22. regexp: match string that contains list of chars - The UNIX and Linux Forums says:
    14 October 2010 at 19:12

    […] some kind of backtracking. And BTW I'm sure that it can be done with regexps! I mean, if you can test if a number is a primer number with regular expressions, I refuse to believe this simple thing can't be […]

  23. regexp: match string that contains list of chars - Page 2 - The UNIX and Linux Forums says:
    14 October 2010 at 20:15

    […] Posted by jimcanoa that it can be done with regexps! I mean, if you can test if a number is a primer number with regular expressions, I refuse to believe this simple thing can't be done Of course it can […]

  24. Programming – Regexp links | Ram's Blog says:
    3 December 2010 at 01:19

    […] A regular expression to check for prime numbers : /^1?$|^(11+?)1+$/ in Perl to check if a number is prime or not […]

  25. 10 ruby one liners « RailsWays says:
    2 June 2011 at 23:52

    […] I read that article too. But again, it’s pretty unreadable and using a Regexp to verify primes just feels […]

  26. /^1?$|^(11+?)1+$/ | sKh updating… says:
    1 September 2011 at 09:38

    […] is prime or not! is it amazing? like a magic! but we know there is no magic in CS.. well, this link has a great explanation.amazing! 极客之美/Geeken, Programming, regex ← Google GO (5) […]

  27. Stupid VIM tricks – Detect prime numbers! | Happy Path says:
    18 November 2011 at 16:37

    […] Some other dude’s blog […]

  28. Matthieu MEZIL says:
    24 April 2013 at 23:08

    Regex et nombres premiers…

    J’aime bien faire des Regex. Je prends souvent ça comme un jeu comme d’autres font des mots croisés et…

  29. Проверка числа на простоту регулярным выражением. | drlexa.ru says:
    25 April 2013 at 17:00

    […] числа на простоту: /^1?$|^(11+?)1+$/. (ссылка на источник: https://www.noulakaz.net/2007/03/18/a-regular-expression-to-check-for-prime-numbers/) Вот скрипт на perl, демонстрирующий работу регулярного […]

  30. Using GREP in InDesign to find prime numbers | A Taste for Desert Landscapes says:
    9 May 2013 at 18:17

    […] with “find” and “change to” in applications such as InDesign. So it was a real pleasure to learn recently that GREP can be used to find prime numbers. (GREP is InDesign’s implementation of “regular […]

  31. Irregular Language and Regular Expressions | Ken Ficara says:
    1 July 2013 at 00:27

    […] other day a friend tweeted about “a regular expression to tell if a number was prime.” I followed the link with my eyebrows raised, because I was fairly sure that was beyond the […]

  32. regex | snowalchemist says:
    20 July 2013 at 21:22

    […] https://www.noulakaz.net/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ […]

  33. Regexp checking for Prime Numbers (TOTD) « says:
    15 October 2013 at 00:27

    […] Folks, I found this resource from Noulakaz pretty […]

  34. How does this regex find primes? | Ask Programming & Technology says:
    3 November 2013 at 20:00

    […] This page claims that this regular expression discovers non-prime numbers (and by counter-example: primes): […]

  35. RegEx Prime Number Check | Amin 's Blog says:
    11 May 2014 at 20:51

    […] зачетную ссылку:https://www.noulakaz.net/2007/03/18/a-regular-expression-to-check-for-prime-numbers/Идея — проверять числа на простоту вот таким вот […]

  36. [奇淫怪巧] 利用正则表达式判断素数 | 易鸣 says:
    11 July 2014 at 05:47

    […] A regular expression to check for prime numbers […]

  37. [奇淫怪巧] 利用正则表达式判断素数 - 博客园 says:
    11 July 2014 at 14:04

    […] A regular expression to check for prime numbers […]

  38. 1p – A regular expression to check for prime numbers – Exploding Ads says:
    12 February 2015 at 20:39

    […] https://www.noulakaz.net/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ […]

  39. A regular expression to check for prime numbers | Ceiba3D Studio says:
    13 February 2015 at 04:27

    […] Source link […]

  40. Les liens de la semaine – Édition #119 | French Coding says:
    16 February 2015 at 15:01

    […] Une expression régulière permettant de déterminer si un nombre est un nombre premier. […]

  41. A regular expression to check for prime numbers - weigandtLabs says:
    17 February 2015 at 18:56

    […] A regular expression to check for prime numbers – http://www.noulakaz.net […]

  42. Ruby isPrime Method | XL-UAT says:
    5 March 2015 at 12:53

    […] You can find a lengthy explanation of this code here: https://www.noulakaz.net/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ […]

  43. Regular Languages Are … well … Regular | Björn Wilmsmann says:
    15 March 2015 at 19:00

    […] few weeks ago I came across an article describing “A regular expression to check for prime numbers“. I was intrigued because processes defined by formal regular languages have an inherently […]

  44. Code – February 2015 | iReidCode says:
    17 July 2015 at 06:56

    […] RegEx […]

  45. 50 best HackerNews posts ever (found using math) | 神刀安全网 says:
    6 January 2016 at 06:46

    […] A regular expression to check for prime numbers by Avinash Meetoo (4 times, 344 points) […]

  46. Formal Correctness | Ryan Dougherty's Blog says:
    2 October 2016 at 20:37

    […] of my students pointed me to this blog post. It claims to show that “checking whether a input number is prime or not” is regular […]

  47. Regular expressions – Everyday Subroutine says:
    31 March 2017 at 09:38

    […] looking around a beautiful regular expression that tests if a number is prime caught my […]

  48. My Top 20 Posts on Noulakaz.net as decided by YOU — Noulakaz says:
    12 September 2019 at 13:01

    […] A regular expression to check for prime numbers (18 March 2007) […]

  49. Primzahlen mit RegEx bestimmen (the nerdy way) › (S)JMP|DE says:
    26 January 2020 at 14:48

    […] habe ich bei Noulakaz einen sehr verwirrenden Weg gefunden eine Zahl auf eine Primzahl hin zu prüfen. Also ob zum […]

  50. Kiểm tra một số có phải số nguyên tố không bằng regex? - Trang Chủ says:
    17 May 2020 at 10:18

    […] link này, tác giả có thá»­ regex bằng Ruby để check cho chắc kèo xem liệu có phải regex […]

  51. A regular expression to check for prime numbers - The web development company Lzo Media - Senior Backend Developer says:
    5 March 2022 at 09:22

    […] Article URL: https://www.noulakaz.net/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ […]

  52. A regular expression to check for prime numbers (2007) | Movilgadget says:
    8 March 2022 at 11:28

    […] Read More […]

  53. Dos curiosidades sobre fórmulas relacionadas con los números primos – Tekins says:
    19 August 2023 at 01:10

    […] artículo de Avinash Meetoo destripa la expresión en sus dos partes, antes y después de la barra vertical (|). La primera […]

  54. Dos curiosidades sobre fórmulas relacionadas con los números primos - Hecho en California con Marcos Gutierrez says:
    19 August 2023 at 03:31

    […] artículo de Avinash Meetoo destripa la expresión en sus dos partes, antes y después de la barra vertical (|). La […]

  55. Dos curiosidades sobre fórmulas relacionadas con los números primos – Celulares, smartphones y tablets says:
    19 August 2023 at 04:39

    […] artículo de Avinash Meetoo destripa la expresión en sus dos partes, antes y después de la barra vertical (|). La primera […]

  56. Dos curiosidades sobre fórmulas relacionadas con los números primos – Pongara News says:
    19 August 2023 at 13:20

    […] artículo de Avinash Meetoo destripa la expresión en sus dos partes, antes y después de la barra vertical (|). La primera […]

  57. Dos curiosidades sobre fórmulas relacionadas con los números primos says:
    19 August 2023 at 16:25

    […] artículo de Avinash Meetoo destripa la expresión en sus dos partes, antes y después de la barra vertical (|). La […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Our Personal Websites

Avinash Meetoo
Christina Meetoo
Anya Meetoo
Kyan Meetoo

Related Posts

  • Answers for my Scheme and Prolog test
  • My top posts of 2012
  • The University of Mauritius Search Engine
  • Mauritius Union menace notre liberté d’expression

Random Posts

  • Two gems from Queue
  • Google in Mauritius: Day 1 on Twitter
  • Analyzing the survey data…
  • Our third PC bought in 2005 in Mauritius

Archives

  • May 2025 (2)
  • April 2025 (4)
  • January 2025 (3)
  • December 2024 (2)
  • November 2024 (2)
  • October 2024 (3)
  • September 2024 (7)
  • August 2024 (1)
  • July 2024 (1)
  • June 2024 (2)
  • May 2024 (3)
  • January 2024 (2)
  • December 2023 (1)
  • October 2023 (1)
  • September 2023 (4)
  • August 2023 (3)
  • July 2023 (1)
  • June 2023 (4)
  • May 2023 (1)
  • April 2023 (1)
  • March 2023 (5)
  • February 2023 (1)
  • December 2022 (1)
  • November 2022 (1)
  • October 2022 (4)
  • August 2022 (4)
  • July 2022 (3)
  • June 2022 (5)
  • May 2022 (5)
  • January 2022 (3)
  • December 2021 (2)
  • November 2021 (1)
  • October 2021 (1)
  • September 2021 (4)
  • August 2021 (2)
  • July 2021 (14)
  • May 2021 (2)
  • April 2021 (4)
  • March 2021 (9)
  • February 2021 (2)
  • January 2021 (1)
  • October 2020 (1)
  • September 2020 (1)
  • August 2020 (2)
  • July 2020 (5)
  • June 2020 (3)
  • May 2020 (5)
  • April 2020 (6)
  • March 2020 (2)
  • February 2020 (2)
  • January 2020 (2)
  • October 2019 (1)
  • September 2019 (2)
  • July 2019 (2)
  • June 2019 (1)
  • May 2019 (3)
  • April 2019 (2)
  • March 2019 (1)
  • February 2019 (1)
  • January 2019 (3)
  • December 2018 (1)
  • October 2018 (3)
  • August 2018 (2)
  • July 2018 (2)
  • June 2018 (1)
  • May 2018 (2)
  • April 2018 (1)
  • February 2018 (1)
  • December 2017 (1)
  • October 2017 (1)
  • September 2017 (1)
  • August 2017 (1)
  • July 2017 (1)
  • May 2017 (4)
  • April 2017 (3)
  • March 2017 (4)
  • February 2017 (5)
  • January 2017 (3)
  • October 2016 (1)
  • September 2016 (1)
  • August 2016 (4)
  • July 2016 (1)
  • June 2016 (1)
  • March 2016 (3)
  • February 2016 (3)
  • January 2016 (1)
  • December 2015 (1)
  • November 2015 (2)
  • September 2015 (1)
  • August 2015 (3)
  • March 2015 (1)
  • December 2014 (1)
  • November 2014 (4)
  • October 2014 (1)
  • March 2014 (2)
  • February 2014 (3)
  • December 2013 (1)
  • October 2013 (1)
  • September 2013 (1)
  • August 2013 (1)
  • July 2013 (1)
  • June 2013 (2)
  • May 2013 (1)
  • March 2013 (3)
  • January 2013 (2)
  • December 2012 (3)
  • November 2012 (4)
  • September 2012 (3)
  • August 2012 (2)
  • July 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (2)
  • February 2012 (1)
  • January 2012 (4)
  • December 2011 (2)
  • November 2011 (1)
  • October 2011 (4)
  • September 2011 (2)
  • August 2011 (1)
  • July 2011 (2)
  • June 2011 (4)
  • April 2011 (7)
  • March 2011 (2)
  • February 2011 (1)
  • January 2011 (3)
  • November 2010 (3)
  • October 2010 (1)
  • September 2010 (2)
  • August 2010 (4)
  • July 2010 (2)
  • June 2010 (1)
  • May 2010 (3)
  • April 2010 (4)
  • March 2010 (3)
  • February 2010 (3)
  • January 2010 (5)
  • December 2009 (2)
  • November 2009 (3)
  • October 2009 (1)
  • September 2009 (5)
  • August 2009 (3)
  • July 2009 (1)
  • June 2009 (3)
  • May 2009 (2)
  • April 2009 (7)
  • March 2009 (12)
  • February 2009 (10)
  • January 2009 (5)
  • December 2008 (4)
  • November 2008 (11)
  • October 2008 (6)
  • September 2008 (7)
  • August 2008 (3)
  • July 2008 (8)
  • June 2008 (6)
  • May 2008 (5)
  • April 2008 (7)
  • March 2008 (6)
  • February 2008 (3)
  • January 2008 (6)
  • December 2007 (11)
  • November 2007 (10)
  • October 2007 (7)
  • September 2007 (9)
  • August 2007 (3)
  • July 2007 (7)
  • June 2007 (8)
  • May 2007 (14)
  • April 2007 (11)
  • March 2007 (18)
  • February 2007 (14)
  • January 2007 (15)
  • December 2006 (16)
  • November 2006 (10)
  • October 2006 (7)
  • September 2006 (8)
  • August 2006 (8)
  • July 2006 (6)
  • June 2006 (4)
  • May 2006 (13)
  • April 2006 (10)
  • March 2006 (11)
  • February 2006 (7)
  • January 2006 (14)
  • December 2005 (8)
  • November 2005 (6)
  • October 2005 (7)
  • September 2005 (2)
  • August 2005 (6)
  • July 2005 (2)
  • June 2005 (6)
  • May 2005 (15)
  • April 2005 (12)
  • March 2005 (3)
  • February 2005 (8)
  • January 2005 (3)
  • December 2004 (1)
  • November 2004 (2)
  • October 2004 (2)
  • September 2004 (3)
  • August 2004 (3)
  • July 2004 (3)
  • June 2004 (3)
  • May 2004 (6)
  • April 2004 (10)
  • March 2004 (12)
Creative Commons License This work is licensed by Avinash Meetoo under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 Unported License.