Tower of Hanoi | Set 2 | Python program for the above approach ; Function to print order of movement of N disks across three rods to place all disks on the third rod from the first - rod using binary representation ; Iterate over the range [ 0 , 2 ^ N - 1 ] ; Print the movement of the current rod ; Driver Code
import math NEW_LINE def TowerOfHanoi ( N ) : NEW_LINE INDENT for x in range ( 1 , int ( math . pow ( 2 , N ) ) ) : NEW_LINE INDENT print ( " Move ▁ from ▁ Rod ▁ " , ( ( x & x - 1 ) % 3 + 1 ) , " ▁ to ▁ Rod ▁ " , ( ( ( x x - 1 ) + 1 ) % 3 + 1 ) ) NEW_LINE DEDENT DEDENT N = 3 NEW_LINE TowerOfHanoi ( N ) NEW_LINE