Perl golf: Print the powers of a number

人盡茶涼 提交于 2019-12-04 16:41:09
moritz

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'
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'
ysth
seq 9|perl -nE'say.37**$_'

26 - Yes, that's cheating. (And yes, I'm doing powers from 1 to 9. 0 to 8 is just silly.)

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.)

perl -e 'print .37**$_,"\n" for 0..9'

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

print join("\n", map { 0.37**$_ } (0..9));
print.37**$_.$/for 0..8

23 strokes if you chop the program before submitting. :-P

Abyss Knight
perl -e "for(my $i = 1; $i < 10; $i++){ print((.37**$i). \"\n\"); }"

Just a quick entry. :)

Fixed to line break!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!