How to resolve the algorithm Pancake numbers step by step in the MAD programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pancake numbers step by step in the MAD 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 MAD programming language

Source code in the mad programming language

            NORMAL MODE IS INTEGER
            VECTOR VALUES ROW = $5(2HP[,I2,4H] = ,I2,S2)*$
            
            INTERNAL FUNCTION(N)
            ENTRY TO P.
            GAP = 2
            ADJ = -1
            THROUGH LOOP, FOR SUM=2, GAP, SUM.GE.N
            ADJ = ADJ + 1
LOOP        GAP = GAP * 2 - 1
            FUNCTION RETURN N + ADJ
            END OF FUNCTION
            
            THROUGH OUTP, FOR R=1, 5, R.G.20
OUTP        PRINT FORMAT ROW, R,P.(R), R+1,P.(R+1), R+2,P.(R+2),
          0     R+3,P.(R+3), R+4,P.(R+4), R+5,P.(R+5)
            
            END OF PROGRAM

  

You may also check:How to resolve the algorithm Align columns step by step in the D programming language
You may also check:How to resolve the algorithm Synchronous concurrency step by step in the Ol programming language
You may also check:How to resolve the algorithm Diversity prediction theorem step by step in the Clojure programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Hy programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Fantom programming language