7 MAGIC PUZZLE 3 X 3 Would take about 7 minutes to generate the solution IntuitivelyThe average number is 5There are 3 numbers in each row, column, diagonalAverage number (5) * # numbers/row .. (3) = 15 5 is probably in the middle (it is) hard code 5 in the middle to speed up the code.
8 TOWERS OF HANOI 3 Towers: left, center, right N disks on left tower All disks have a different diameterA smaller disk must be on top of a bigger disk; it can be alone tooWe want to move the N disks to the center tower
10 TOWERS OF HANOI Solution (general case: N >= 2): To move N disks from A to B using CMove (N-1) disks from A to C using B (recursive call)Move 1 disk from A to BMove (N-1) disks from C to B using A (recursive call)Base Case (only 1 disk to move from A to B; no need for C here):
11 TOWERS OF HANOIProblem: Can we move N disks from tower A to tower B using tower C?Yes or No answer + printing out all the stepsPredicate move takes 4 arguments: the number of disks, and 3 towers
12 TOWERS OF HANOI Base case: Move 1 disk from A to B move(1,A,B,_) :- write(‘Move top disk from ‘),write(A),write(‘ to ‘),write(B),nl.
13 TOWERS OF HANOI General case: Move N disks from A to B using C move(N,A,B,C) :-N > 1,M is N – 1,move(M,A,C,B),move(1,A,B,_),move(M,C,B,A).
Your consent to our cookies if you continue to use this website.