Hanoi =: 3 : 0 NB. Left argument x is the number of rings NB. Right argument y is a pair of numbers from 0 1 2 NB. for the start and end poles. NB. Output is a boxed list of moves (m,n) of the top ring NB. from pole m to pole n. NB. For example NB. 3 Hanoi 0 2 NB. for 3 rings to go from pole 0 to pole 2. It returns NB. ----------------------------- NB. |0 2|0 1|2 1|0 2|1 0|1 2|0 2 | NB. ----------------------------- : n =. x NB. just a rename n_1 =. <:x NB. a name for n-1 first =. {. NB. local function for first element of a list last =. {: NB. local function for last element of a list if. n = 1 do. < y NB. return y as a boxed pair else. v =. (-. 0 1 2 e. y) # 0 1 2 NB. locate intermediate pole (n_1 Hanoi (first y),v), (1 Hanoi y), n_1 Hanoi v, last y end. ) NB. ====== Timing and Space ======== ts =: 6!:2 , 7!:2@] NB. ts '16 Hanoi 2 1' --- boxed output NB. 2.23474 5.77082e6 NB. ts '16 Hanoi1 2 1' --- list output NB. 1.4762 5.77075e6 NB. ts '16 Hanoi2 2 1' --- stripped of comments NB. 1.26181 2.62502e6 --- Note halved the space NB. But the J-software supplied function H NB. in Studio - Labs - The Tower of Hanoi NB. ts '0 1 2 H 16' NB. 0.865871 2.62483e6 NB. NB. The lab goes on to develop a function, H1, that has NB. only a single, rather than a double recursion: NB. ts '0 1 2 H1 16' NB. 0.00546773 3.14906e6 NB. and then develops iterative alternatives and other NB. ways of shortening the execution time to get NB. ts 'H5 16' NB. 0.00490621 1.57382e6 NB. =================================