Perl golf: Print the powers of a number

2019-04-12 15:50发布

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

8条回答
闹够了就滚
2楼-- · 2019-04-12 16:23
perl -le'map{print.37**$_}0..8'

31 characters - I don't have 5.10 to try out the obvious improvement using "say" but this is 28:

perl -E'map{say.37**$_}0..8'
查看更多
迷人小祖宗
3楼-- · 2019-04-12 16:29

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'
查看更多
来,给爷笑一个
4楼-- · 2019-04-12 16:29
perl -e 'print .37**$_,"\n" for 0..9'

If you add -l to options you can skip the ,"\n" part

查看更多
唯我独甜
5楼-- · 2019-04-12 16:34
print join("\n", map { 0.37**$_ } (0..9));
查看更多
家丑人穷心不美
6楼-- · 2019-04-12 16:35

Just for fun in Perl 6:

  1. 28 characters:

    perl6 -e'.say for .37»**»^9'
    
  2. 27 characters:

    perl6 -e'say .37**$_ for^9'
    

(At least based on current whitespace rules.)

查看更多
Emotional °昔
7楼-- · 2019-04-12 16:38
perl -e "for(my $i = 1; $i < 10; $i++){ print((.37**$i). \"\n\"); }"

Just a quick entry. :)

Fixed to line break!

查看更多
登录 后发表回答