The Nightstar Zoo

Nightstar IRC Network - irc.nightstar.net
It is currently Sat Oct 21, 2017 7:13 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Tue Feb 25, 2003 7:01 pm 
perl -le "(1x++$_)!~/^(11+)\1+$/&&print while 1"

1) What does this do?
2) Do you understand it?
3) Can you explain how it works?
4) Were you able to do this without running it?

Enquiring minds want to knwo.


Top
  
 
PostPosted: Wed Feb 26, 2003 3:00 am 
Pi wrote:
1) What does this do?
2) Do you understand it?
3) Can you explain how it works?
4) Were you able to do this without running it?

Enquiring minds want to knwo.


1. Got me.
2. No.
3. No.
4. Wha?


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 12:09 pm 
1) It prints out a list of prime numbers

2) It's an obfuscated code example, it's designed deliberately to be hard to read
and misleading

3) From left to right:

"perl"
the executable used to invoke PERL

"-le"
command line switches
'l' will put each "print" statement on it's own line
'e' is used to tell PERL that you want to execute the rest of the line as PERL code

"(1x++$_)"
this is obfuscated code that repeats the character '1' as many times as set in
the variable '$_', then it increments $_ by 1, for example when $_ is 5, (1x++$_)
would generate 11111 and then store 6 in $_

"!~"
This is a PERL regular expression operator for non-matches, it compares the
left-hand side of the operator to the expression on the right-hand side and
returns true if the LHS doesn't match the regexp on the RHS

"/^(11+)\1+$/"
This is the regular expression that determines whether or not the number is
prime based on the number of 1's generated

"&& print"
This prints out the value of $_ if everything to the left of the && returned a
true value (in this case, as long as the series of 1's doesn't match the above
pattern)

"while 1"
This creates an infinite loop around the line, PERL will continue to add 1 to $_
and check if it's prime until the script is terminated

4) No, I did not run it.


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 12:17 pm 
kreely wrote:
4) No, I did not run it.

Perhaps not, but I bet you had help.


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 12:23 pm 
Yup...had access to some books.

You never stated that we couldn't use books or available resources.

Besides, I like a good challenge. It's like Mindtrap. You have to think.


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 12:30 pm 
No, I didn't. You glossed over one point that I would think is of algorithmic significance...
kreely wrote:
"/^(11+)\1+$/"
This is the regular expression that determines whether or not the number is prime based on the number of 1's generated

...But perl regexps are cryptic enough _without_ explanation, so I'll award credit. ;)

Nice job.


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 12:47 pm 
Promise to do better next time.


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 1:59 pm 
Offline
Vorpal Bunny Slipper
Vorpal Bunny Slipper

Joined: Sun May 12, 2002 2:54 am
Posts: 2707
This just confirms my belief that Perl is an icky, horrible language.

_________________
Scharr, scharr, verscharr das Gebein, grab es tief unten im Keller ein.
Später dann graben es andere aus, und nennen dein Haus das Knochenhaus.
Scharr, scharr, verscharr das Gebein, leg auch ihre weißen Schädel hinein.
Mit Beton gießt du es aus, das Fundament vom Knochenhaus.
Scharr, scharr, verscharr das Gebein, da ist noch Platz, da paßt noch wer rein.
Hier tobte sich der Teufel aus, unten im Keller im Knochenhaus.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 6:23 pm 
And that some people have more time than they can profitably use.


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 11:11 pm 
:P

jealous?


Top
  
 
 Post subject:
PostPosted: Wed Feb 26, 2003 11:18 pm 
Offline
Energizer Bunny
User avatar

Joined: Wed May 22, 2002 12:24 am
Posts: 1634
To be exact, the regex pattern

^(11+)\1+$

returns a large group if and only if there are exactly m>1 (one plus at least one) groups of n>1 1's (again, one plus at least one) from the start to the end of the line... so that m>1 * n>1 = m*n, so the number of 1's is composite if it matches.

Note that I know precisely jack about perl.

Vorn


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group