29.3k views
3 votes
Cutting a stick A stick n inches long needs to be cut into n 1-inch pieces. Outline an algorithm that performs this task with the minimum number of cuts if several pieces of the stick can be cut at the same time. Also give a formula for the minimum number of cuts.

User Wryan
by
5.1k points

1 Answer

3 votes

Answer:

The algorithm and its formula are given below

Step-by-step explanation:

Algorithm:

Start

Input Length of stick in inches

while(1) do // Loop runs until break statement execute

if(inchesOfStick == 1)do // If there is 1 inch stick

break // exit from loop

end if

if(inchesOfStick % 2 == 0) do // if the inches remaining is even

inchesOfStick /= 2 // Forula to cut

end if

else if(inchesOfStick % 2 == 1) do // if the inches remaining is odd

inchesOfStick = (inchesOfStick + 1) / 2 // Formula to cut

increment minimumCuts

end else

end while

print minimum number of cuts

end program

User Davorak
by
5.4k points