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
 [reply] 

Regular expressions can be made to solve diophantine equations!
Please examine:
# 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?  [reply] [d/l] 
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  [reply] 

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 nonmatching 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.
 [reply] 

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
 [reply] 



 [reply] 

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)
 [reply] 

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).  [reply] 

@ARGV = qwI forgot to login before I posted it.;
 [reply] 
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
 [reply] 

No, 1 is not prime by definition.
A good source for math info is MathWorld, wonderful site.
Just my 2 cents, gjb
 [reply] 