On Jan 22, Frank said:
>On Tue, Jan 22, 2002 at 12:47:02PM -0500, Jeff wrote:
>> [Peter: what does "Aspirat primo Fortuna labori" mean? I've gotten a
>> partial translation, but I'm not satisfied with it.]
>
>It means "Fortune favours your first effort".. Mia Culpa
s/Mia/Mea/, I think. Thanks for the translation, though!
>> A thorough word-regex might be needed here, but let's not get into that.
>
>I'll shut up then ;)
I won't. I usually use something like this:
/(\w(?:[-']+\w+)*)/
It matches words like jack-o-lantern, let's, and will-o'-the-wisp. It's
thorough, but only by fortune -- it's not a very solid attempt. I mean,
it matches blit--'fo''-gah as a word.
>Great! How does this work?
>
>$|=1;
>$_=1;
>(1x$_) !~ /^(11+)\1+$/ && print "$_\n" while $_++;
First, do you know what it does? It prints prime numbers. What is the
property of prime numbers? They cannot be factored (save for 1 and the
number itself). That means that you can't partition the number into TWO
or more piles of TWO or more "things".
(11+)
can be written as
(1{2,})
which matches two or more things, and
(1{2,})\1+
matches those things two or more times -- once at the (1{2,}), and one or
more times at the \1+.
The regex is anchored to the front and back of the string to avoid false
positives -- 7 is prime, so "1111111" =~ /^(11+)\1+$/ will fail, but if we
had left out the ^ and $ anchors, the regex would have gladly matched only
six of the 1's.
The regex uses backtracking and backreferences to employ factoring. The
string it matches against is "1" repeated as many times as the number is
-- for 7, the string is "1111111". The regex then matches two or more
1's, and then tries to match that SAME number of 1's, one or more times:
^ (11+) \1+ $
1111111 fail
111111 fail
11111 fail
1111 fail
111 111 fail
11 1111 fail
fail
fail
Therefore, 7 can't be factored by this regex, and it is prime (so it gets
printed). Inversely, 9 can be factored:
^ (11+) \1+ $
111111111 fail
11111111 fail
1111111 fail
111111 fail
11111 fail
1111 1111 fail
111 111111 pass
9 can be factored (when (11+) matches "111") so it is not prime, and it is
not printed.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]