How to resolve the algorithm Pancake numbers step by step in the AWK programming language
How to resolve the algorithm Pancake numbers step by step in the AWK programming language
Table of Contents
Problem Statement
Adrian Monk has problems and an assistant, Sharona Fleming. Sharona can deal with most of Adrian's problems except his lack of punctuality paying her remuneration. 2 pay checks down and she prepares him pancakes for breakfast. Knowing that he will be unable to eat them unless they are stacked in ascending order of size she leaves him only a skillet which he can insert at any point in the pile and flip all the above pancakes, repeating until the pile is sorted. Sharona has left the pile of n pancakes such that the maximum number of flips is required. Adrian is determined to do this in as few flips as possible. This sequence n->p(n) is known as the Pancake numbers. The task is to determine p(n) for n = 1 to 9, and for each show an example requiring p(n) flips. Sorting_algorithms/Pancake_sort actually performs the sort some giving the number of flips used. How do these compare with p(n)? Few people know p(20), generously I shall award an extra credit for anyone doing more than p(16).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pancake numbers step by step in the AWK programming language
Source code in the awk programming language
# syntax: GAWK -f PANCAKE_NUMBERS.AWK
# converted from C
BEGIN {
for (i=0; i<4; i++) {
for (j=1; j<6; j++) {
n = i * 5 + j
printf("p(%2d) = %2d ",n,main(n))
}
printf("\n")
}
exit(0)
}
function main(n, adj,gap,sum) {
gap = 2
sum = 2
adj = -1
while (sum < n) {
adj++
gap = gap * 2 - 1
sum += gap
}
return(n + adj)
}
You may also check:How to resolve the algorithm Increment a numerical string step by step in the min programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the Raku programming language
You may also check:How to resolve the algorithm CUSIP step by step in the Quackery programming language
You may also check:How to resolve the algorithm Long year step by step in the Perl programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the Delphi programming language