I'm not sure if this goes in "Craft" or "Obfuscated", but it
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).
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.
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)