Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/'

OK so I thought i'd elaborate a little on how this works since the monks from the chatterbox took time to explain it to me :)

So you have a number, say 6, and "write" it as a sequence of 1 ie (111111) .
If you write it (11)(11)(11) "you just found that 6 = 3 * 2", as moritz explained it.

ELISHEVA explains the math behind this :
if there is a repeating group of the same number of ones, then a factorization is possible and hence the number can't be prime.

It means that if you can write the number as M groups of K ones, then it factorizes as :

M * [ Sum(p=1->k) 1 ] which really is just M * K

Which means that our number can be divided by M (or K), and therefore it's not prime. Shmem gives an example: i.e. for m = 1763, the group found would be 11111111111111111111111111111111111111111 repeated 43 times - not prime

So how does the regexp implement that ? We can decompose it like this :

m/ ^ # start of line 1? # the number 1, zero or one time $ # end of line | # alternation ^ # start of line ( # remember the match in \1 11+? # the number 1, then the number 1 once or more t +imes, but the less time possible ) # \1+ # the matched sequence, once or more. $ /x ;
So the real trick is in (11+?). In a standalone context, it will only match 11. That's because +? means "once or more but the minimum number of times". On the other hand, ^(11+?)$ will match a whole sequence of ones from begining to end.

Here (11+?) is followed by \1+$ which means : itself, once or more, until the end of the line .
So what happens is that the "minimum number of times" that's contained in +? is seen from the \1+$ that is after it in the expression
So when no match occurs, the engine will go back to the (11+?) and try again.

I understand that's what backtracking is. Eventually the regexp engine will try every grouping of ones and fine none.
Since it needs at least two groupings to match (enforced by the + in \1+ ), a prime number will not match.

The only problem is that for some numbers you get a Complex regular subexpression recursion limit (32766) exceeded error. My guess was that it happens when the engine has to try more than 32766 number of times, ie the first fail appears for a number that needs K>32766. That's not the case though, since prime number 32779 does not yield the error. 65558 does, though. I went on and brutforced it, to find that the error appears first with 65536, which is 2 * 32768, which computes since it needs two groups to match...

In reply to Re^2: To Findout Prime number by ZlR
in thread To Findout Prime number by babug_prg

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [thezip]: Our cat is single-handedly (pawed-ly?) solving our field mouse problem. About four critters in the last week alone.
    [thezip]: Amazing speed and stealth in that one.
    [GotToBTru]: You can't tunafish, but some seafood companies can tunafish. More fun with English.
    [virtualsue]: a veterinarian told me to cut down on my cat's food. i said, when i do that she just catches more mice
    [thezip]: My dogs are also excellent mousers, at least that's what they tell me.
    [GotToBTru]: our beagle has dispatched 3 rabbits to the Great Hutch in the Sky since March.
    [thezip]: I can always tell when the dogs have been mousing, as there is a concentric ring of mud around the end of their snouts when they do.

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (16)
    As of 2017-11-17 19:53 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (272 votes). Check out past polls.