Towers of Hanoi Problem
you have N rings of increasing size and three pegs. Initially the three rings are
stacked in order of decreasing size on the first peg. You can move them between pegs but you must never stack a big ring onto a smaller one. What is the sequence of moves to move from all the rings from the first to the the third peg.
answers:
hanoi( N ):-
move( N, left, middle, right ).
move(1,X,Y,_) :-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Find Factoria (N!) of a number
factorial(0, 1). % Factorial of 0 is 1.
factorial(N, FactN) :-
N > 0, % N is positive
Nminus1 is N - 1, % Calculate N minus 1
factorial(Nminus1, FactNminus1), % recursion
FactN is N * FactNminus1. % N! = N * (N - 1)!
No comments:
Post a Comment
Don't forget to Reply if you like my post.
If you don't Reply Post will Die........