// TOWER of HANOI PROBLEM
// Triple level recirsion (difficult to follow)
// 3 pegs with n disks on the first peg (smallest
on top largest on bottom)
// move disks one at a time till smallest on top and
largest on the bottom of 3rd peg
// when moving a larger disks cannot rest on top of
smaller disk
// example of recursion for the tower of hanoi problem
// complicated -- keep as a thought exercise when
you are bored
import java.lang.*;
import javax.swing.*;
public class Application1
{
public static void main(String[] args)
{
int n=3; // number if disks
function(n,"left","right","middle");
JOptionPane.showMessageDialog(null,"finished");
}
public static void function(int n,String source,String
destination,String extra)
{
if(n == 1)
{
System.out.println("move disk
from "+ source +" to "+destination);
}
else
{
function(n-1,source,extra,destination);
function(1,source,destination,extra);
function(n-1,extra,destination,source);
}
}
}