prolog programs for finding factorial of numbers and generating a Fibonacci series. - - Artificial intelligence

No comments
  •  program for finding a factorial of given number (N!)

Program
fact(X,Y):-
    X is  0,
    Y is 1;
    X>0,
    N is X-1,
    fact(N,G),
    Y is X*G.

Output
?- fact(6,X).
X = 720 .
?- fact(8,X).
X = 40320

  • program for generating a Fibonacci series.
Program
fib(0,0).
fib(1,1).
fib(N,F):-
   N>1,
   N1 is N-1,
   N2 is N-1,
   fib(N1,F1),
   fib(N2,F2),
   F is F1+F2.

Output
%?- fib(5,F).
%F = 16 .

No comments :

Post a Comment