Search code examples
perlmathcode-golf

Perl golf: Print the powers of a number


What's the shortest Perl one-liner that print out the first 9 powers of a hard-coded 2 digit decimal (say, for example, .37), each on its own line?

The output would look something like:

1
0.37
0.1369
[etc.]

Official Perl golf rules:

  1. Smallest number of (key)strokes wins
  2. Your stroke count includes the command line

Solution

  • With perl 5.10.0 and above:

    perl -E'say 0.37**$_ for 0..8'
    

    With older perls you don't have say and -E, but this works:

    perl -le'print 0.37**$_ for 0..8'
    

    Update: the first solution is made of 30 key strokes. Removing the first 0 gives 29. Another space can be saved, so my final solution is this with 28 strokes:

    perl -E'say.37**$_ for 0..8'