Professional Documents
Culture Documents
Lucas in 1883, Consists of Three Vertical Pegs and Some Number N of Disks
Lucas in 1883, Consists of Three Vertical Pegs and Some Number N of Disks
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
if
(no_of_di
sc==1)
Stop
C PROGRAM & CODDING
#include<stdio.h>
void towers(int,char,char,char);
int main()
Int num;
scanf(“%d”,&num);
towers(num,’A’,’B’,’C’);
return 0;
if(num==1)
return;
towers(num-1,from,aux,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.
DISADVANTAGE
Consume more storage space as recursion call and automatic variable as
stored in a stack.
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 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.
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