Skip to content

Instantly share code, notes, and snippets.

@ebongzzang
Created July 5, 2018 04:29
Show Gist options
  • Save ebongzzang/a13abd0db810f5cc367e52e156b3acc3 to your computer and use it in GitHub Desktop.
Save ebongzzang/a13abd0db810f5cc367e52e156b3acc3 to your computer and use it in GitHub Desktop.
Tower of hanoi In groovy
def main() {
def reader = new Scanner(System.in)
int a = reader.nextInt()
BigInteger count = (2 ** a) -1
println(count)
boolean canPrint = a <= 20
def hanoiTower
hanoiTower = { int num, int from, int to, int aux ->
if (num == 1) {
System.out.printf("%d %d\n",from ,to)
return
}
hanoiTower(num - 1, from, aux, to)
System.out.printf("%d %d\n",from ,to)
hanoiTower(num - 1, aux, to, from)
}
if(canPrint) {
hanoiTower.call(a, 1, 3, 2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment