Giter VIP home page Giter VIP logo

tower-of-hanoi's Introduction

Simpler Iterative Tower of Hanoi

This algorithm is so simple it makes larger stacks quite easy to do manually

manual-hanoi.webm

It's quite similar to the already existing iterative solutions, but I've found it's not necessary to know which peg the disks are on, or which peg a disk will move to.

All that needs to be known is the direction (UP/DOWN), which alternates for each disk and is determined by the parity of the number of disks.

  • For an even number of disks, the first only ever moves UP, the second DOWN, third UP, etc.
  • For an odd number of disks, the first only ever moves DOWN, the second UP, third DOWN, etc.

Then we just do some calculations to get the number each disk will move on, and how many moves it will make in total.

  • The first disk will move a total of 2^(n-1) times, the second 2^(n-2) times, etc. where n is the number of disks.
  • The amount to increase its move position by (to then determine which move that disk will move on) is 2^(i+1), where i is the current disk number.

So for example with 4 disks labelled A - D:

A moves 2^3 times (8), and increases by 2^1 (2)
B moves 2^2 times (4), and increases by 2^2 (4)
C moves 2^1 times (2), and increases by 2^3 (8)
D moves 2^0 times (1), and increases by 2^4 (16)

It is pretty similar to binary counting and the Gray code solutions on Wikipedia, but when solving it I chose not to do any research beyond knowing the rules of the game. If I had looked up the solutions beforehand, I probably wouldn't have come up with this one.

This probably isn't the best implementation for this algorithm, as it's pretty hacky, but at least it's extremely fast. From some simple benchmarks I've found it's ~3x faster than other iterative solutions, and ~10x faster than recursive ones.

Also only 110 lines of assembly gets generated with -Ofast, so have fun blowing up your CPU! (without arg parsing it's 58..)

Install

Copy-paste into a shell with wget and gcc installed

wget https://raw.githubusercontent.com/644/tower-of-hanoi/main/hanoi.c; gcc hanoi.c -ohanoi -Ofast

Then run with

./hanoi 4

Outputs

A UP
B DOWN
A UP
C UP
A UP
B DOWN
A UP
D DOWN
A UP
B DOWN
A UP
C UP
A UP
B DOWN
A UP

I think the longest part here is the startup time from the executing shell

time ./hanoi 20 >/dev/null

real	0.063
user	0.059
sys	0.003
cpu	99.23

disks.webm

tower-of-hanoi's People

Contributors

644 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.