this post was submitted on 12 Jun 2025
332 points (96.9% liked)

Fuck AI

3099 readers
665 users here now

"We did it, Patrick! We made a technological breakthrough!"

A place for all those who loathe AI to discuss things, post articles, and ridicule the AI hype. Proud supporter of working people. And proud booer of SXSW 2024.

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Tartas1995@discuss.tchncs.de 6 points 1 day ago* (last edited 1 day ago) (4 children)

Hey for anyone who doesn't know how to solve tower of Hanoi, there is a simple algorithm.

1.  |.  |
2.  |.  |
3.  |.  |

Let's say, we want to move the center rod.

Count the stack of disks that you need to move: e.g. 3

If it is even, start with placing the first disk on the spot that you don't want to move the tower to. If it is odd, start with placing the first disk on the spot that you want to move the tower to.


|.  |.  |
2.  |.  |
3.  1.  |

|.  |.  |
|.  |.  |
3.  1.  2


|.  |.   |
|.  |.   1
3.  |.   2


|.   |.  |
|.   |.  1
|.   3.  2

Now the 2 stack is basically a new Hanoi tower.

That tower is even and we start with placing the first disk on the spot that we don't want to land on


|.  |.  |
|.  |.  |
1.  3.  2


|.  |.  |
|.  2.  |
1.  3.  |

|.  1.  |
|.  2.  |
|.  3.  |

And we solved the tower. It is that easy

[–] ZDL@lazysoci.al 2 points 1 day ago (3 children)
[–] Tartas1995@discuss.tchncs.de 4 points 1 day ago (2 children)

It works the same way.

1  |  |
2  |  |
3  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 |  |
12 |  |

|  |  |
2  |  |
3  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 |  |
12 |  1

|  |  |
|  |  |
3  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 |  |
12 2  1

|  |  |
|  |  |
3  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 1  |
12 2  |

|  |  |
|  |  |
|  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 1  |
12 2 3

|  |  |
|  |  |
1  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 |  |
12 2  3

|  |  |
|  |  |
1  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 |  2
12 |  3

|  |  |
|  |  |
|  |  |
4  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  1
11 |  2
12 |  3

|  |  |
|  |  |
|  |  |
|  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  1
11 |  2
12 4  3

|  |  |
|  |  |
|  |  |
|  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 1  2
12 4  3


|  |  |
|  |  |
|  |  |
2  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 1  |
12 4  3

|  |  |
|  |  |
1  |  |
2  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 |  |
12 4  3

|  |  |
|  |  |
1  |  |
2  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 |  |
11 3  |
12 4  |

|  |  |
|  |  |
|  |  |
|  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  |  |
10 2  |
11 3  |
12 4  1

|  |  |
|  |  |
|  |  |
|  |  |
5  |  |
6  |  |
7  |  |
8  |  |
9  1  |
10 2  |
11 3  |
12 4  |

|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
6  |  |
7  |  |
8  |  |
9  1  |
10 2  |
11 3  |
12 4  5

And so on... As you can see, when there was a 3 stack in the right pole, and moved the 4, for the solution to create space for the 5, we move the 3 stack as if it is just a 3 tower and we will end up with a 4 stack tower, allowing us to move the 5 and now we need to move the 4 stack on the 5. As the 4 stack is even, we would start by moving the 1 to the left stack, placing the 2 on the 5 and then the 1 on the 2, creating a 2 stack, now we can move the 3 on the left pole, now we solve the 2 stack, as it is even, we move the 1 to the 4 and the 2 on the 3 and then the 1 on the 2, creating a 3 stack and allowing us to move the 4 onto the 5. Now we solve the 3 stack onto the 4. It is odd, so we solve as we solve the previous 3 tower.

A bigger tower is just solving a 1 smaller tower basically twice.

So for solving 12, you solve 11 and move the 12 to the spot, to solve 11 again. To solve 11, you solve 10 and move the 11 and solve 10 again....

[–] ZDL@lazysoci.al 2 points 1 day ago (1 children)

I was mostly hoping to have you print out all 1000 or so moves as a prank. :D

[–] kogasa@programming.dev 1 points 8 hours ago* (last edited 7 hours ago)

1000 or so moves

Ok, this nerd-snipes me a bit. The real point of the Towers of Hanoi problem, at least if you're doing it in a discrete math class, is to count exactly the number of moves for n discs. Call this number f(n).

To make this post easier to read, we will label the discs from 1 to n, with 1 being the bottom disc and n being the top. We will also label the pegs A, B, and C, with A being the starting peg and C being the target peg. This lets us describe moves in a compact notation: 3: A -> C means disc 3 moves from A to C.

For n=0, we vacuously need 0 moves to win.

For n=1, we need just 1 move: 1:A->C.

For n=2, we can do it in 3 moves, so f(2) = 3. The sequence is:

  • 2: A->B # unblock the next move
  • 1: A->C # move 1 to its final location
  • 2: B->C # move 2 to its final location

Now suppose we know f(n) for n between 1 and N, with N >= 2. For n=N+1, we can move N+1: A -> C and attempt to use our strategy for moving the remaining N discs from A to C, shuffling N+1 around as needed. Since it's the smallest disc, we can always move it to any pillar, and any time we want to move something to the pillar it's on, it must be moved first. Let's see how that plays out for N=2:

  • 3: A->C # move the N+1 disc
  • 2: A->B # start the N-disc strategy
  • 3: C->B # unblock the next step
  • 1: A->C # next step of N-disc strategy
  • 3: B->A # unblock the next step
  • 2: B->C # last step of N-disc strategy
  • 3: A->C # finish the N+1-disc problem

We hazard a guess that every step of the N-disc strategy is preceded by a move of the N+1 disc, plus one final move. In other words, f(N+1) = 2f(N) + 1. Some careful analysis will justify this guess.

Now we have a recurrence relation for f(n), but how to solve it? A classical technique is "guess the formula magically, then prove it by induction." It's certainly doable here if you compute a few values by hand:

f(2) = 3

f(3) = 2(3) + 1 = 7

f(4) = 2(7) + 1 = 15

f(5) = 2(15) + 1 = 31

f(6) = 2(31) + 1 = 63

You'll probably see it right away: f(n) = 2^n - 1. Indeed, we can prove this by induction: the base case n=2 holds as f(2) = 3 = 2^(2) - 1, and if f(n) = 2^n - 1 then f(n+1) = 2f(n) + 1 = 2(2^n - 1) + 1 = (2^(n+1) - 2) + 1 = 2^(n+1) - 1 as desired.

We may conclude f(12) = 2^(12) - 1 = 4095. In other words, with 12 discs, exactly 4095 moves are required.

--

Bonus: an alternative to the "guess and prove" technique that is generalizable to a broad class of recurrence relations. The technique is called "generating functions." Given the sequence f(n), we consider the formal power series

F(x) = f(0) + f(1)x + f(2)x^(2) + ... + f(n)x^(n) + ...

This F(x) is the "ordinary generating function" for the sequence f(n). Strictly speaking, it may not be a well-defined function of x since we haven't made any assumptions about convergence, but we will assume (for now, and prove later) that the set of such formal power series behaves algebraically much like we expect. Namely, given the recurrence relation above, we can write:

F(x) - f(0) - f(1)x = f(2)x^(2) + f(3)x^(3) + f(4)x^(4) + ... + f(n)x^n + ...

= (2f(1) + 1)x^(2) + (2f(2) + 1)x^(3) + ... + (2f(n-1) + 1)x^(n) + ...

= 2x(f(1)x + f(2)x^(2) + ... + f(n)x^(n)) + (x^(2) + x^(3) + ... + x^(n) + ...)

= 2x(F(x) - f(0)) + x^(2)/(1-x)

In our case, we have f(0) = 0, f(1) = 1, f(2) = 3 so we can write more succinctly:

F(x) - x = 2xF(x) + x^(2)/(1-x)

Solving for F,

F(x)(1 - 2x) = x + x^(2)/(1-x)

= x(1 + x/(1-x))

F(x) = x(1 + x/(1-x))/(1 - 2x)

= x/(2x^(2) - 3x + 1)

Ok, great. We've found that our generating function, convergence notwithstanding, is that rational function. We can use partial fraction decomposition to write it as

F(x) = 1/(1 - 2x) - 1/(1-x)

which has the advantage of telling us exactly how to compute the coefficients of the Taylor series for F(x). Namely,

1/(1-x) = 1 + x + x^(2) + ... + x^(n) + ...

1/(1 - 2x) = 1 + 2x + 4x^(2) + ... + 2^(n) x^(n) + ...

So F(x) = (1-1) + (2-1)x + (4-1)x^(2) + ... + (2^(n)-1)x^(n) + ...

The nth coefficient of the Taylor series for F about 0 is 2^(n)-1, and by the definition of F as the ordinary generating function for f(n), we have f(n) = 2^(n) - 1. (The rigorous justification for ignoring convergence here still needs to be done; for now, this can be seen as a useful magic trick.)