Created
July 5, 2018 04:29
-
-
Save ebongzzang/a13abd0db810f5cc367e52e156b3acc3 to your computer and use it in GitHub Desktop.
Tower of hanoi In groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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