"be consistent" PerlMonks

### Primes

by Mo (Initiate)
 on Apr 05, 2000 at 18:11 UTC Need Help??

`   1: perl -e 'while(\$l++<99){\$_.=x;print \$l,\$/if! /^x\$|^(xx+)\1+\$/}'`

Replies are listed 'Best First'.
RE: Primes
by stephen (Priest) on Apr 06, 2000 at 02:00 UTC
I'm not sure if this goes in "Craft" or "Obfuscated", but it is cool.

I suppose it would be possible for some deranged individual to create a Math::Regexp module, that does all math calculations via regular expressions... First person to do so wins Stephen's Too Much Time On Their Hands Award (TMTOTH).

stephen

```# for the equation:  3x + 2y + 10z = 50

(\$x,\$y,\$z,\$ans) = (3,2,10,50);
\$xpat = 0 x \$x;
\$ypat = 0 x \$y;
\$zpat = 0 x \$z;

if ((0 x \$ans) =~ /^ ((?:\$xpat)+) ((?:\$ypat)+) ((?:\$zpat)+) \$/x) {
print "x = ", length(\$1)/\$x, "\n";
print "y = ", length(\$2)/\$y, "\n";
print "z = ", length(\$3)/\$z, "\n";
}
You can change this by making them * instead of +, or *? or +?. Isn't it sick?
RE: Primes
by PipTigger (Hermit) on Apr 06, 2000 at 05:03 UTC
How does this work? I don't understand what it's doing and I seem to break it when I change anything. I like it tho =). TTFN & Shalom.

-PipTigger
It's not too bad. What it does it count from 1 to 99. Each time through, it adds another x to the string.

The tricky part is in the regex. The first time through, \$_ is just one x, so the first part of the match works. But the match is reversed, so the number doesn't print.

On subsequent passes, that part fails. The second half of it grabs a certain number of x's. Then, it looks for the same number of x's, repeated a certain number of times. For example, with two, the first grouping matches, but the second doesn't. Reverse the non-matching flag, and it prints the number. On the third try, same thing.

On the fourth pass, the first part matches two x's, and so does the second. Reverse that flag, and it won't print. Basically, all this does it use the grouping feature of the regex engine to perform a factorial operation.

I think primes is really cool! I would like to fully understand how it works and I have 2 questions about the second part of the regex: 1. why isn't (xx+) greedy? I mean why doesn't it match the whole string of x in \$_? 2. I thought it was possible substitute \1 with \$1: ^(xx+)\$1+\$, but this regex doesn't work correctly. why? TIA for any explanation. marcos
Agreed; the only change I could make without breaking it is swapping the "<99" with ">-1"-- although that did get the desired result: my tweaked version spews primes until you hit Ctrl-C.

(Yeah, it's lame, but it gave me a brief moment of extreme joy in an otherwise very dreary day.)

I tweaked it like you did, and set it up running on background for 3 days... To my surprise, when I returned today its at 66,683 and running slooooow as hell (takes like a minute or so to print the next iteration).

I wonder how many loops its jumping through to get to the next answer?
On a side note: /usr/games/bin/primes got to the same result in like 5 seconds... (just outta curiosity)
The obfuscated code version would be.
```my\$q;\$q=sub{print ++\$#_,qq()};\$_=qq{\$[};\$,="\n";1while(s{^(\$[)}{\$1\$1}mo&&(@_=m{\$[}gom)&&(m{^(\$[\$[+)\1+\$}om)?1:&\$q);
```
this actually prints all prime numbers ( until you ^c it).
@ARGV = qw|I forgot to login before I posted it.|;
Re: Primes
by monsieur_champs (Curate) on Jun 24, 2003 at 17:24 UTC

Nice, but this does not print "1" to begin. Why? "1" is not a prime number?

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Just Another Perl Monk

No, 1 is not prime by definition.

A good source for math info is MathWorld, wonderful site.

Just my 2 cents, -gjb-

Create A New User
Node Status?
node history
Node Type: perlcraft [id://6940]
Approved by root
help
Chatterbox?
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2018-04-22 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (83 votes). Check out past polls.

Notices?