I have written code to solve the Tower of Hanoi problem

By 8 Vynce on July 24, 2008

Any successful program (or piece of a program) in any language to solve the famous tower game counts.

With or without recursion.

Embed Claim Make a related claim

Discussion (7)

http://elihu.myopenid.com/

3 Elihu who agreed, says

/*
Jim Snow
3-3-1998
*/

#include <stdio>
#include <stdlib>

void hanoi(int n, int a, int b, int c) // a=beginning b=middle c=destination
{
if (n==1)
printf("\nmove %d to %d", a, c);
else
{
hanoi(n-1, a, c, b);
printf("\nmove %d to %d", a, c);
hanoi(n-1, b, a, c);
}
}

void main()
{
int n=1;
char input[20];

printf("\n | | | towers");
printf("\n - | | of");
printf("\n --- | | hanoi -");
printf("\n ----- | | Enter the number of rings: ");

fgets(input, 20, stdin);
n=atoi(input);

if (n>0)
hanoi(n, 1, 2, 3);

printf("\n");
}

Make a related claim 5 months ago (link)
http://vynce.myopenid.com/

8 Vynce who disagreed, says

jyte needs a way to hide spoilers. ( :

are you Jim Snow, Elihu?

Make a related claim 5 months ago (link)
http://elihu.myopenid.com/

3 Elihu who agreed, says

That's me. Sorry, I didn't realize posting a solution would spoil the fun.

Make a related claim 5 months ago (link)
https://me.yahoo.com/bbwritesut

1 RangerGordon who agreed, says

If I remember correctly, the easiest solution was recursive.

Although, now that I think about it ... is there even a nonrecursive solution?

Make a related claim 5 months ago (link)
http://vynce.myopenid.com/

8 Vynce who disagreed, says

I was kidding about the spoiler warning.

and yes, there are non-recursive solutions. follow the link in the description for more information.

Make a related claim 5 months ago (link)
http://elihu.myopenid.com/

3 Elihu who agreed, says

A word of warning: solving for n=64 will cause the world to end.

Make a related claim 5 months ago (link)
http://darch.myopenid.com/

6 D'Archangel who hasn't voted, says

"Although, now that I think about it ... is there even a nonrecursive solution? "

Yes. It is provable that every recursive algorithm has an iterative counterpart.

D'A

Make a related claim 5 months ago (link)
Sign in in to leave a comment.