Pancake numbers: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add MAD |
Not a robot (talk | contribs) Add Cowgol |
||
Line 104: | Line 104: | ||
}</lang> |
}</lang> |
||
{{out}} |
{{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|Cowgol}}== |
|||
{{trans|C}} |
|||
<lang cowgol>include "cowgol.coh"; |
|||
sub pancake(n: uint8): (r: uint8) is |
|||
var gap: uint8 := 2; |
|||
var sum: uint8 := 2; |
|||
var adj: int8 := -1; |
|||
while sum < n loop |
|||
adj := adj + 1; |
|||
gap := gap * 2 - 1; |
|||
sum := sum + gap; |
|||
end loop; |
|||
r := n + adj as uint8; |
|||
end sub; |
|||
# print 2-digit number |
|||
sub print2(n: uint8) is |
|||
if n<10 then |
|||
print_char(' '); |
|||
else |
|||
print_char(n/10 + '0'); |
|||
end if; |
|||
print_char(n%10 + '0'); |
|||
end sub; |
|||
# print item |
|||
sub print_item(n: uint8) is |
|||
print("p("); |
|||
print2(n); |
|||
print(") = "); |
|||
print2(pancake(n)); |
|||
print(" "); |
|||
end sub; |
|||
var i: uint8 := 0; |
|||
while i < 4 loop |
|||
var j: uint8 := 1; |
|||
while j < 6 loop |
|||
print_item(i*5 + j); |
|||
j := j + 1; |
|||
end loop; |
|||
print_nl(); |
|||
i := i + 1; |
|||
end loop;</lang> |
|||
{{out}} |
|||
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5 |
<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( 6) = 7 p( 7) = 8 p( 8) = 9 p( 9) = 10 p(10) = 11 |