Pancake numbers: Difference between revisions
m
syntax highlighting fixup automation
m (→exhaustive search, with examples: syntax coloured) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16:
=={{header|AWK}}==
{{incomplete|AWK|Show examples requiring p(1..9) flips}}
<syntaxhighlight lang="awk">
# syntax: GAWK -f PANCAKE_NUMBERS.AWK
# converted from C
Line 40:
return(n + adj)
}
</syntaxhighlight>
{{out}}
<pre>
Line 51:
{{incomplete|C|Show examples requiring p(1..9) flips}}
{{trans|Go}}
<
int pancake(int n) {
Line 73:
}
return 0;
}</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 83:
{{incomplete|C++|Show examples requiring p(1..9) flips}}
{{trans|C}}
<
#include <iostream>
Line 105:
}
return 0;
}</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 116:
{{trans|C}}
<
sub pancake(n: uint8): (r: uint8) is
Line 160:
print_nl();
i := i + 1;
end loop;</
{{out}}
Line 172:
{{incomplete|D|Show examples requiring p(1..9) flips}}
{{trans|C}}
<
int pancake(int n) {
Line 192:
writeln;
}
}</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 200:
=={{header|F_Sharp|F#}}==
<
// Pancake numbers. Nigel Galloway: October 28th., 2020
let pKake z=let n=[for n in 1..z-1->Array.ofList([n.. -1..0]@[n+1..z-1])]
Line 210:
[1..9]|>List.iter(fun n->let i,g=pKake n in printfn "Maximum number of flips to sort %d elements is %d. e.g %A->%A" n i g [1..n])
</syntaxhighlight>
{{out}}
<pre>
Line 229:
===Maximum number of flips only===
{{trans|C}}
<
Function pancake(n As Integer) As Integer
Dim As Integer gap = 2, sum = 2, adj = -1
Line 245:
Next n
Sleep
</syntaxhighlight>
{{out}}
<pre>
Line 257:
===Maximum number of flips only===
{{trans|Phix}}
<
import "fmt"
Line 279:
fmt.Println()
}
}</
{{out}}
Line 297:
Not particularly fast - Julia is about 3 seconds faster on the same machine.
<
import (
Line 396:
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</
{{out}}
Line 419:
===Fast approximation===
{{trans|Go|Original algorithm from [[#Phix|Phix]]}}
<
private static int pancake(int n) {
int gap = 2;
Line 441:
}
}
}</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 452:
Uses a standard breadth-first search using a queue. Note that because java is very verbose at defining classes, we instead had <code>pancake</code> return a <code>Map.Entry<List<Integer>, Integer></code> directly, rather than a dedicated named class. This is arguably bad practice, but keeps the snippet terse.
<
import static java.util.stream.Collectors.toList;
Line 498:
}
}
}</
{{out}}
Line 515:
=={{header|Julia}}==
{{trans|Phix}}
<
gap, gapsum, adj = 2, 2, -1
while gapsum < len
Line 529:
i % 5 == 0 && println()
end
</
<small>Note that pancake(20) and above are guesswork</small>
<pre>
Line 541:
=== with examples ===
Exhaustive search, breadth first method.
<
goalstack = collect(1:len)
stacks, numstacks = Dict(goalstack => 0), 1
Line 563:
println("pancake(", lpad(i, 2), ") = ", rpad(steps, 5), " example: ", example)
end
</
<pre>
pancake( 1) = 0 example: [1]
Line 580:
===Fast approximation===
{{trans|Go|Original algorithm from [[#Phix|Phix]]. The printing in main was adapted to use something more idiomatic.}}
<
var gap = 2
var sum = 2
Line 596:
val lines = results.chunked(5).map { it.joinToString(" ") }
lines.forEach { println(it) }
}</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 606:
Using classic breadth-first search with running queue.
<
fun pancake(n: Int): PancakeResult {
Line 631:
}
}
</syntaxhighlight>
{{out}}
Line 651:
{{trans|C}}
<
VECTOR VALUES ROW = $5(2HP[,I2,4H] = ,I2,S2)*$
Line 668:
0 R+3,P.(R+3), R+4,P.(R+4), R+5,P.(R+5)
END OF PROGRAM</
{{out}}
Line 681:
{{trans|Phix}}
This is the translation of the second version (5th dec 2020). It differs from the first version for p(19).
<
func pancake(n: int): int =
Line 699:
line.addSep(" ")
line.add &"p({n:>2}) = {pancake(n):>2}"
if n mod 5 == 0: (echo line; line.setLen(0))</
{{out}}
Line 711:
We used a "TableRef" rather than a "Table" to optimize some assignments (Nim uses copy semantic when assigning). We also defined a function "partialReversed" rather than using the "reversed" function and a concatenation. These optimizations reduce the running time from about 21 seconds to about 17 seconds on our small laptop.
<
type
Line 751:
for n in 1..10:
let (steps, example) = pancake(n)
echo &"p({n:>2}) = {steps:>2} example: ", example.join(", ")</
{{out}}
Line 766:
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 808:
my ($a,$b) = pancake2($n);
say "pancake($n) = $a example: $b";
}</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 834:
(ie the p(20) shown below is actually pure guesswork, with a 50:50 chance of being correct)<br>
Note that several other references/links disagree on p(17) and up.</small>
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pancake</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 850:
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"p(%2d) = %2d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 869:
=== modified (5th Dec 2020) ===
It seems someone has recently modified A058986/b058986.txt so here is a matching modified version, which would make p(20) either 23 or 24.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pancake</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 885:
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"p(%2d) = %2d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 896:
=== exhaustive search, with examples ===
{{trans|Julia}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">visitor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">stack</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*unused*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">stacks</span><span style="color: #0000FF;">)</span>
Line 933:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"p(%d) = %d, example: %v (of %,d, %s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">eg</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sz</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
Note that we are only allowed to flip the left hand side, so [eg] we cannot solve p(3) by flipping the right hand pair.<br>
Line 968:
{{trans|Java}}
{{works with|Python|3.7}}
<
import time
Line 1,010:
print(f"\nTook {time.time() - start:.3} seconds.")
</syntaxhighlight>
{{out}}
Line 1,029:
===Maximum number of flips only===
{{trans|C}}
<syntaxhighlight lang="raku"
sub pancake(\n) {
Line 1,037:
}
for (1..20).rotor(5) { say [~] @_».&{ sprintf "p(%2d) = %2d ",$_,pancake $_ } }</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 1,046:
===Maximum number of flips plus examples using exhaustive search===
{{trans|Go}}
<syntaxhighlight lang="raku"
my @goalStack = (my \numStacks = $ = 1)..n ;
my %newStacks = my %stacks = @goalStack.Str, 0 ;
Line 1,066:
say "The maximum number of flips to sort a given number of elements is:";
for 1..9 -> $j { given pancake($j) { say "pancake($j) = $_[1] example: $_[0]" }}</
{{out}}
Line 1,084:
{{trans|Go}}
{{trans|Phix}}
<
pad= center('' , 10) /*indentation. */
say pad center('pancakes', 10 ) center('pancake flips', 15 ) /*show the hdr.*/
Line 1,099:
sum= sum + gap /*add the GAP to the SUM. */
end /*adj*/
return n +adj -1 /*return an adjusted adjustment sum. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,130:
{{trans|C}}
Does not show examples requiring p(n) flips, since that is beyond the capabilities of Ring.
<
for n = 1 to 9
see "p(" + n + ") = " + pancake(n) + nl
Line 1,146:
end
return n + adj
</syntaxhighlight>
Output:
<pre>
Line 1,163:
{{incomplete|Ruby|Show examples requiring p(1..9) flips}}
{{trans|C}}
<
gap = 2
sum = 2
Line 1,181:
end
print "\n"
end</
{{out}}
<pre>p( 1) = 0 p( 2) = 1 p( 3) = 3 p( 4) = 4 p( 5) = 5
Line 1,195:
Clearly, for non-trivial 'n', the number of flips required for the pancake sorting task will generally be more as no attempt is being made there to minimize the number of flips, just to get the data into sorted order.
<
var pancake = Fn.new { |n|
Line 1,215:
}
System.print()
}</
{{out}}
Line 1,230:
Note that map iteration order is undefined in Wren and so the examples are (in effect) randomly chosen from those available.
<
// Converts a string of the form "[1, 2]" into a list: [1, 2]
Line 1,293:
Fmt.print("pancake($d) = $-2d example: $n", i, steps, example)
}
System.print("\nTook %(System.clock - start) seconds.")</
{{out}}
|