Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

INTRODUCTION

The Tower of Hanoi, a puzzle invented by French mathematician Edouard


Lucas ´ in 1883, consists of three vertical pegs and some number n of disks,
all with a hole in the center that allows them to be stacked on the pegs. The
disks are all of different radii, and we imagine that they are numbered 1
through n in increasing order of size. They are initially stacked in order on one
of the pegs, with disk n on the bottom and 1 on the top. The object is to
transport this tower from the starting peg to a destination peg, moving one
disk at a time, and always placing a disk on either an empty peg or on a larger
disk. The third peg is used to temporarily hold disks during the process. See
[1] or [5] for an early discussion of this puzzle. It is well known that there is a
unique minimum-move solution that takes 2n−1 steps. It is most easily
described recursively: for n > 0, recursively move a tower of n−1 disks from
the initial peg to the temporary peg; move disk n from the initial peg to the
destination peg; and then recursively move the tower of n−1 disks from the
temporary peg to the destination peg. If M0(n) is the number of moves made,
we have the recurrence relation M0(n) = M0(n−1) + 1 + M0(n−1) for n > 0
with M0(0) = 0, which has the solution M0(n) = 2n − 1 given above. Many
variations on this simple puzzle soon followed, with features such as colored
disks, multiple pegs, and restricted moves. In fact, Lucas himself invented a
version with five pegs and a stack of disks of four different colors [3]. The
puzzles presented here, most of which were invented and patented by Victor
Mas color of Bayville, New York, all involve multiple stacks of disks. Each stack
has a unique color, but otherwise the stacks are identical. As before, we
imagine that the disks in each stack are numbered from 1 to n in increasing
order of size. Each stack must be moved from its assigned initial peg to its
destination peg, always moving just one disk at a time. Unlike the multi-stack
Towers of Antwerpen puzzle posed by Derrick Wood [7] and solved by Steven
Minsker [4], we enforce a strict size rule: a disk can only be placed on an
empty peg or on top of a strictly larger disk of any color. For n > 1 this
necessitates that we have at least two more pegs than stacks. For each puzzle,
a minimum-move algorithm is desired, along with a proof of minimality.
RULLS:-

1.)Only one disk can be move data time.

2.)A disk can only be moved if it is the upper most disk on a peg, and it can
only be place don a destination peg, if it is smaller than the most disk
currently on the destination peg.

Solution for solving 3 peg Tower Of Hanoi An algorithm that solves the Tower
of Hanoi problem is shown below. As we’ll describe briefly in the following
analysis section, this algorithm is in fact an “optimal” solution, in that it solves
the problem in the absolute minimum number of moves.

1) Move the smallest disk to the peg it has not recently come from (or if it’s
the very first move, move the smallest disk to the nontarget peg if there are an
even number of disks,other wise, move it to the target peg). 2) Move an other
disk legally (there will only be one possibility 3) Repeat both steps until all
disks have been moved to their final location.
ALGORITHM
1. Start.
2. Read no of disc from user.
3. Call tower function by passing values “no. of discs, char(A, B, C)”.
4. if (no_of_disc==1) Then display”move disc 1 from “char(A, B, C)” to
“char(A, B, C)”” Else call tower function by passing values “no. of discs-1,
char(A, C, B)” display “move disc “no_of_disc” from “char(A, B, C)” to
“char(A, B, C)”” call tower function by passing values “no. of discs-1,
char(B, A, C)”
5. Stop.

FLOWCHART
Start

Read no of disc from user

Call tower function by passing


values “no. of discs, char(A, B, C)”

if
(no_of_di
sc==1)

call tower function by passing


values “no. of discs-1, char(A,
C, B)”

display”move disc 1 from display “move disc “no_of_disc”


“char(A, B, C)” to “char(A, B, from “char(A, B, C)” to “char(A,
C)”” B, C)””

call tower function by passing


values “no. of discs-1, char(B,
A, C)”

Stop
C PROGRAM & CODDING

#include<stdio.h>

void towers(int,char,char,char);

int main()

Int num;

printf(“Enter the number of disc:”);

scanf(“%d”,&num);

printf(“The sequence of moves involved in the tower of hanoi are:\n”);

towers(num,’A’,’B’,’C’);

return 0;

void towers(int num,char from,char to,char aux)

if(num==1)

printf(“\n move disc 1 from %c to %c”,from,to);

return;

towers(num-1,from,aux,to);

printf(“\n move disc %d from %c to %c,num,from,to);

towers(num-1,aux,to,from);

}
OUTPUT
ADVANTAGE
 It make program code compact which is easier to write and understand.

 It is used with the data structure such as linked list, stack,queue etc.

 It is useful if a solution to a problem in repetitive from


.
 The compact code in a recursion simplified the compilation as less
number of line need to be compiled.

DISADVANTAGE
 Consume more storage space as recursion call and automatic variable as
stored in a stack.

 It is less efficient in comparison to normal program in case of speed any


execution time.

 Special care need to be taken for stopping condition in a recursion


function.

 If the recursion call are not checked ,the computer may run out of
memory.
CONCLUSION

We show that the Towers of Hanoi puzzle, with 3 pegs, and N disks, has
a solution.

The as been proven to be optimal.

We also examine do never of the Tower Of Hanoi puzzle, called the


Revers Puzzle, which introduction 4th peg to the puzzle.

The Frame-Stewart algorithm solves this puzzle, however, the it’s has
yet to be proven whether Frame’s.

Conjecture for choosing how to choose “k”, for breaking up the initial
problem of N disks, into 2 sub problems of size N-K.

So the Revers puzzle remain as open problem.

Lastly, we briefly touché don some search currently underway,for trying


to disprove the Frame-Stewart algorithm, for larger values of N
REFERENCE
 http://www.lovelycoding.org/2012/04/project-in-c.html

 http://www.cppforschool.com/project/c.html
 http://www.mathology.net/mathology/vis_articolo.asp?id=40&lang=en
g
 http://www.scs.carleton.ca/research/tech_reports/2004/TR-04-10.pdf

• Software used: -

• TDM compiler

• TCC compiler

• Turbo C

• Dev Cpp

You might also like