Pancake numbers: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: optimized/simplified the pancake function DO loop, added/changed comments. |
SqrtNegInf (talk | contribs) Added Perl |
||
Line 388: | Line 388: | ||
p(11) = 13 p(12) = 14 p(13) = 15 p(14) = 16 p(15) = 17 |
p(11) = 13 p(12) = 14 p(13) = 15 p(14) = 16 p(15) = 17 |
||
p(16) = 18 p(17) = 19 p(18) = 20 p(19) = 21 p(20) = 23 </pre> |
p(16) = 18 p(17) = 19 p(18) = 20 p(19) = 21 p(20) = 23 </pre> |
||
=={{header|Perl}}== |
|||
<lang perl>use strict; |
|||
use warnings; |
|||
use feature 'say'; |
|||
sub pancake { |
|||
my($n) = @_; |
|||
my ($gap, $sum, $adj) = (2, 2, -1); |
|||
while ($sum < $n) { $sum += $gap = $gap * 2 - 1 and $adj++ } |
|||
$n + $adj; |
|||
} |
|||
my $out; |
|||
$out .= sprintf "p(%2d) = %2d ", $_, pancake $_ for 1..20; |
|||
say $out =~ s/.{1,55}\K /\n/gr;</lang> |
|||
{{out}} |
|||
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5 |
|||
p( 6) = 7 p( 7) = 8 p( 8) = 9 p( 9) = 10 p(10) = 11 |
|||
p(11) = 13 p(12) = 14 p(13) = 15 p(14) = 16 p(15) = 17 |
|||
p(16) = 18 p(17) = 19 p(18) = 20 p(19) = 21 p(20) = 23</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |