Perl Christmas Quiz 2009

James Coupe james at
Tue Dec 1 00:31:03 GMT 2009

Chris Jack <chris_jack at> wrote:
>Abigail abigail at wrote
>> On Mon, Nov 30, 2009 at 06:24:12PM +0000, Chris Jack wrote:
>> > 7) Write a one line program that takes a non-negative integer as an
>> >argument
>> > and prints the square root when the answer's an integer.
>> >
>> > Restrictions: the perl line should be a regular expression.
>> Just a regular expression? Regular expressions don't print, so that would
>> be impossible.
>Pedant. Perl regular expressions allow execution of arbitrary code
>blocks - which is why I put restrictions on which "ordinary" functions
>you were allowed to use. The actual square root algorithm, however,
>should only use the "normal" regular expression bestiary.

It's not obvious to me what you're regarding as ordinary and normal, but
I get something like...

$ perl -e '("1" x $ARGV[0]) =~ m/^(1*)((??{$1x(length($1)-1)})$)(?(2)(?{print length $1}))/' 4

I don't recommend trying it on large numbers.  1024 was about as high as
my boredom threshold could tolerate on this box.

James Coupe

More information about the mailing list