0% found this document useful (0 votes)
15 views26 pages

Ai

Uploaded by

saritha.pachala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views26 pages

Ai

Uploaded by

saritha.pachala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

INDEX

S.No Description Page.no


1 Implementation of DFS for Water Jug problem using PROLOG
2 Implementation of BFS for TicTacToe problem using JAVA
3 Implementation of TSP using heuristic approach using PROLOG
4 Implementation of Towers of Hanoi problem using PROLOG
5 Implementation of Monkey Banana Problem using PROLOG
6 Implementation of 8 Queens problem using Heuristic Approach
7 Implementation of 8 Puzzle problem using PROLOG
8 Implementation of Hill Climbing Algorithm using LISP
9 A) Implementation of Sets using LISP
B) Implementation of Reversing a given List in LISP
10 Program to implement Simulated Annealing using JAVA
Additional Experiments
11 Implementation of Expert System with Backward chaining using
PROLOG
12 Program to findout Union and Intersection of two lists
13 Implementation of Factorial and Fibonacci of a given number
Exp.no 1: Implementation of DFS for Water Jug problem using PROLOG
Aim: To implement water jug problem using DFS in PROLOG
Source code:
dnwnfi

water_jug (X, Y) :- X>4,Y<3,write('4L water jug overflowed'), nl .


water_jug (X, Y):-X<4,Y>3,write('3L water jug overflowed'),nl.
water_jug (X, Y):-X>4,Y>3,write('Both water jug overflowed'),nl.
water_jug (X, Y):-(X=:=0, Y=:=0, nl,write('4L:0 & 3L:3(Action:fill 3L jug)'),YY is 3,water_jug(X,YY));
(X=:=0,Y=:=0,nl,write('4L : 4 & 3L:0(Action:full 4L jug)'),XX is 4,water_jug(XX,Y));
(X=:=2,Y=:=0,nl,write('4l:2 & 3L:0(Action:Goal state is reached)'));
(X=:=4,Y=:=0,nl,write('4L:4 & 3L:0(Action:pour water from 4L to 3L jug)'), XX is X-3,YY is
3,water_jug(XX,YY));
(X=:=0,Y=:=3,nl,write('4L:0 & 3L:3(Action:pour water from 3L to 4L jug)'),XX is 3,YY is
0,water_jug(XX,YY));
(X=:=1,Y=:=3,nl,write('4L:1 & 3L:3(Action:Empty 3L jug)'),YY is 0, water_jug(X,YY));
(X=:=3,Y=:=0,nl,write('4L:3 & 3L:0(Action:fill 3L jug)'),YY is 3, water_jug(X,YY));
(X=:=3,Y=:=3,nl,write('4L:4 & 3L:2(Action:pour water from 3L to 4L jug until 4L jug is
full)'),XX is X+1,YY is Y-1,water_jug(XX,YY));
(X=:=1,Y=:=0,nl,write('4L:0 & 3L:1(Action:pour water from 4L to 3L jug)'),XX is Y,YY is
X,water_jug(XX,YY));
(X=:=0,Y=:=1,nl,write('4L:4 & 3L:1(Action:fill 4L jug)'),XX is 4,water_jug(XX,Y));
(X=:=4,Y=:=1,nl,write('4L:2 & 3L:3(Action:pour water from 4L to 3L jug until 3L jug is
full)'),XX is X-2,YY is Y+2,water_jug(XX,YY));
(X=:=2,Y=:=3,nl,write('4L:2 & 3L:0(Action:Empty 3L jug)'),YY is 0,water_jug(X,YY));
(X=:=4,Y=:=2,nl,write('4L:0 & 3L:2(Action:Empty 4L jug)'),XX is 0,water_jug(XX,Y));
(X=:=0,Y=:=2,nl,write('4L:2 & 3L:0(Action:pour water from 3L to 4L jug)'),XX is Y,YY is
X,water_jug(XX,YY)).
Output:
water_jug(0,0).
4L:0 & 3L:3 (Action: Fill 3L jug)
4L:3 & 3L:0 (Action: Pour water from 3L to 4L jug)
4L:3 & 3L:3 (Action: Fill 3L jug)
4L:4 & 3L:2 (Action: Pour water from 3L to 4L jug until 4L jug is full)
4L:0 & 3L:2 (Action: Empty 4L jug)
4L:2 & 3L:0 (Action: Pour water from 3L jugto 4L jug)
4L:2 & 3L:0 (Action: Goal state is reached)
true .
water_jug(3,0).
4L:3 & 3L:3 (Action: Fill 3L jug)
4L:4 & 3L:2 (Action: Pour water from 3L to 4L jug until 4L jug is full)
4L:0 & 3L:2 (Action: Empty 4L jug)
4L:2 & 3L:0 (Action: Pour water from 3L jugto 4L jug)
4L:2 & 3L:0 (Action: Goal state is reached)
true .

water_jug(3,2).
false.
Exp.no 2: Implementation of BFS for TicTacToe problem using JAVA
Aim: To implement of BFS for TicTacToe problem using JAVA
Source code:
import java.util.*;

public class TicTacToe {


static String[] board;
static String turn;
static String checkWinner() {
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
line = board[2] + board[4] + board[6];
break;
}
if (line.equals("XXX")) {
return "X";
} else if (line.equals("OOO")) {
return "O";
}
}
for (int a = 0; a < 9; a++) {
if (Arrays.asList(board).contains(String.valueOf(a + 1))) {
break;
} else if (a == 8) {
return "draw";
}
}
// To enter the X or O at the exact place of the board
System.out.println(turn + "sturn;Enter a slot number to place" + turn + "in");
return null;
}
static void printBoard() {
System.out.println("|___|___|___|");
System.out.println("|" + board[0] + "|" + board[1] + "|" + board[2] + "|");
System.out.println("|______|");
System.out.println("|" + board[3] + "|" + board[4] + "|" + board[5] + "|");
System.out.println("|______|");
System.out.println("|" + board[6] + "|" + board[7] + "|" + board[8] + "|");
System.out.println("|______|");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
board = new String[9];
turn = "X";
String Winner = null;
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a + 1);
}
System.out.println("Welcome to 3X3 Tic Tac Toe");
printBoard();
System.out.println("X will play first.Enter a slot number to place X in: ");
while (Winner == null) {
int numInput;
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println(("Invalid input;re-enter slot number:"));
continue;
}
} catch (InputMismatchException e) {
System.out.println("Invalid input;re-enter slot number:");
continue;
}
if (board[numInput - 1].equals(String.valueOf(numInput))) {
board[numInput - 1] = turn;
if (turn.equals("X")) {
turn = "O";
} else {
turn = "X";
}
printBoard();
Winner = checkWinner();
} else {
System.out.println("Slot already taken;re-enter slot number:");
}
}
if (Winner.equalsIgnoreCase("Draw")) {
System.out.println("Its a draw!Thanks for playing.");
}

else {
else {
System.out.println("Congratulations!" + Winner + "S have won!Thanks for playing.");
}
}
}
Output:
Welcome to 3X3 Tic Tac Toe
|___|___|___|
|1|2|3|
|______|
|4|5|6|
|______|
|7|8|9|
|______|
X will play first.Enter a slot number to place X in:
5
|___|___|___|
|1|2|3|
|______|
|4|X|6|
|______|
|7|8|9|
|______|
Osturn;Enter a slot number to placeOin
3
|___|___|___|
|1|2|O|
|______|
|4|X|6|
|______|
|7|8|9|
|______|
Xsturn;Enter a slot number to placeXin
9
|___|___|___|
|1|2|O|
|______|
|4|X|6|
|______|
|7|8|X|
|______|
Osturn;Enter a slot number to placeOin
1
|___|___|___|
|O|2|O|
|______|
|4|X|6|
|______|
|7|8|X|
|______|
Xsturn;Enter a slot number to placeXin
2
|___|___|___|
|O|X|O|
|______|
|4|X|6|
|______|
|7|8|X|
|______|
Osturn;Enter a slot number to placeOin
8
|___|___|___|
|O|X|O|
|______|
|4|X|6|
|______|
|7|O|X|
|______|
Xsturn;Enter a slot number to placeXin
7
|___|___|___|
|O|X|O|
|______|
|4|X|6|
|______|
|X|O|X|
|______|
Osturn;Enter a slot number to placeOin
6
|___|___|___|
|O|X|O|
|______|
|4|X|O|
|______|
|X|O|X|
|______|
Xsturn;Enter a slot number to placeXin
4
|___|___|___|
|O|X|O|
|______|
|X|X|O|
|______|
|X|O|X|
|______|
Its a draw!Thanks for playing.
Exp.no 3: Implementation of TSP using heuristic approach using PROLOG
Aim: To implement TSP using heuristic approach using Prolog.
Source code:
road(gnt,vij,9).
road(ama,gnt,3).
road(ama,vij,6).
road(vizag,ama,5).
road(ama,vizag,5).
road(hyd,ama,4).
road(hyd,vizag,8).
get_road(Start,End,Visited,Result):-
get_road(Start,End,[Start],0,Visited,Result).
get_road(Start,End,Waypoints,DistanceAcc,Visited,TotalDistance):-
road(Start,End,Distance),
reverse([End/Waypoints],Visited),
TotalDistance is DistanceAcc+Distance.
get_road(Start,End,Waypoints,DistanceAcc,Visited,TotalDistance):-
road(Start,Waypoint,Distance),\+member(Waypoint,waypoints),
NewDistanceAcc is DistanceAcc+Distance,
get_road(Waypoint,End,[Waypoint/Waypoints],NewDistanceAcc,Visited,TotalDistance).
Output:
get_road(hyd,vizag,Visited,Result).
Visited = [vizag/[hyd]],
Result = 8

get_road(gnt,vij,Visted,Result).
Visted = [vij/[gnt]],
Result = 9
Exp.no 4: Implementation of Towers of Hanoi problem using PROLOG
Aim: To implement of Towers of Hanoi using PROLOG
Source code:
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).

Output:
move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
true
Exp.no 5: Implementation of Monkey Banana Problem using PROLOG
Aim: To implement of Monkey Banana Problem using PROLOG
Source code:
move(state(middle,onbox,middle,hasnot),
grasp,
state(middle,onbox,middle,has)).
move(ssate(P,onfloor,P,H),
climb,
state(P,onbox,P,H)).
move(state(P1,onfloor,P1,H),
drag(P1,P2),
state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),
walk(P1,P2),
state(P2,onfloor,B,H)).
canget(state(_,_,_,has)).
canget(state1):-
move(state1,_,state2),
canget(state2).

Output:
canget(state(atdoor,onfloor,atwindow,has)).
true.
Exp.no 6: Implementation of 8 Queens problem using Heuristic Approach
Aim: To implement of 8 Queens problem using Heuristic Approach.
Source code:
import java.util.Arrays;
class Queens{
static final int N=8;
static boolean isSafe(int[][] board,int row,int col){
for(int x=0;x<col;x++){
if(board[row][x]==1){
return false;
}
}
for(int x=row,y=col;x>=0&&y>=0;x--,y--){
if(board[x][y]==1){
return false;
}
}
for(int x=row,y=col;x<N&&y>=0;x++,y--){
if(board[x][y]==1){
return false;
}
}
return true;
}
static boolean solveNQueens(int[][] board,int col){
if(col==N){
for(int[] row:board){
System.out.println(Arrays.toString(row));
}
System.out.println();
return true;
}
for(int i=0;i<N;i++){
if(isSafe(board,i,col)){

board[i][col]=1;
board[i][col]=1;
if(solveNQueens(board,col+1)){
return true;
}
board[i][col]=0;
}
}
return false;
}
public static void main(String[] args) {
int[][] board=new int[N][N];
if(!solveNQueens(board,0)){
System.out.println("No solution found");
}
}
}
Output:
[1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
Exp.no 7: Implementation of 8 Puzzle problem using PROLOG
Aim: To implement of 8 puzzle problem using PROLOG
Source code:
test(Plan):-
write('Initial state:'),nl,
Init=[at(tile4,1),at(tile3,2),at(tile8,3),at(empty,4),at(tile2,5),at(tile6,6),at(tile5,7),at(tile1,8),at(tile7,9)],
write_sol(Init),
Goal=[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),at(tile7,8),at(tile8,9)],nl,
write('Goal state:'),nl,
write(Goal),nl,nl,
solve(Init,Goal,Plan).
solve(State,Goal,Plan):-
solve(State,Goal,[],Plan).
is_movable(X1,Y1):-(1 is X1-Y1);(-1 is X1-Y1);(3 is X1-Y1);(-3 is X1-Y1).
solve(State,Goal,Plan,Plan):-
is_subset(Goal,State),nl,
write_sol(Plan).
solve(State,Goal,Sofar,Plan):-
act(Action,Preconditions,Delete,Add),
is_subset(Preconditions,State),
\+member(Action,Sofar),
delete_list(Delete,State,Remainder),
append(Add,Remainder,NewState),
solve(NewState,Goal,[Action|Sofar],Plan).
act(move(X,Y,Z),
[at(X,Y),at(empty,Z),is_movable(Y,Z)],
[at(X,Y),at(empty,Z)],
[at(X,Z),at(empty,Y)]).
is_subset([H|T],Set):-
member(H,Set),
is_subset(T,Set).
is_subset([],_).
delete_list([H|T],Curstate,NewState):-
remove(H,Curstate,Remainder),

delete_list(T,Remainder,NewState).
delete_list(T,Remainder,NewState).
delete_list([],Curstate,Curstate).
remove(X,[X|T],T).
remove(X,[H|T],[H|R]):-
remove(X,T,R).
write_sol([]).
write_sol([H|T]):-
write_sol(T),
write(H),nl.
append([H|T],L1,[H|L2]):-
append(T,L1,L2).
append([],L,L).
member(X,[X|_]).
member(X,[_|T]):-
member(X,T).

Output:
test(plan).
Initial state:
at(tile7,9)
at(tile1,8)
at(tile5,7)
at(tile6,6)
at(tile2,5)
at(empty,4)
at(tile8,3)
at(tile3,2)
at(tile4,1)

Goal state:
[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),at(tile7,8),at(tile8,9)]

false.
Exp.no 8: Implementation of Hill Climbing Algorithm using LISP
Aim: To implement of Hill Climbing Algorithm using LISP
Source code:
(setf (get 's 'neighbors) '(a d)
(get 'a 'neighbors) '(s b d)
(get 'b 'neighbors) '(a c e)
(get 'c 'neighbors) '(b)
(get 'd 'neighbors) '(s a e)
(get 'e 'neighbors) '(b d f)
(get 'f 'neighbors) '(e))

(setf (get 's 'coordinates) '(0 3)


(get 'a 'coordinates) '(4 6)
(get 'b 'coordinates) '(7 6)
(get 'c 'coordinates) '(11 6)
(get 'd 'coordinates) '(3 0)
(get 'e 'coordinates) '(6 0)
(get 'f 'coordinates) '(11 3))
(defun straight-line-distance (node1 node2)
"Compute the straight line distance between two nodes"
(let ((coord1 (get node1 'coordinates))
(coord2 (get node2 'coordinates)))
(sqrt (+ (expt (- (first coord1) (first coord2)) 2)
(expt (- (second coord1) (second coord2)) 2)))))
(defun extend-path (path)
"Extend the path by consing on new neighbor nodes that dont result in loops"
(mapcar #'(lambda (new-node) (cons new-node path))
(remove-if
#'(lambda (neighbor) (member neighbor path))
(get (first path) 'neighbors))))
(defun closerp (node1 node2 target)
"Returns true if node1 is closer to target than node2, else false"
(< (straight-line-distance node1 target)
(straight-line-distance node2 target)))
(defun hill-climb (start finish
&optional (queue (list (list start))))
"Perform hill climbing search for path from start to finish"
(cond ((endp queue) nil)
((eq finish (first (first queue)))
(reverse (first queue)))
(t
(print queue)
(hill-climb
start
finish
(append (sort (extend-path (first queue))
#'(lambda (p1 p2)
(closerp (first p1) (first p2) finish)))
(rest queue))))))
Output:
(hill-climb 's 'f)
((S))
((A S) (D S))
((B A S) (D A S) (D S))
((C B A S) (E B A S) (D A S) (D S))
((E B A S) (D A S) (D S))
(S A B E F)

(hill-climb 's 'd)


((S))
(S D)
Exp.no 9:
A) Implementation of Sets using LISP
Aim: To implement of Sets using LISP
Source code:
(defparameter *mynewset* ());;emptylist
(adjoin 1 *mynewset*);;*mynewset*isnow(1)
(adjoin 2 *mynewset*);;*mynewset*isnow(1 2)
;adjoin did not change the original set
;so it remains same
(write *mynewset*);;(1 2)
(terpri);;prints a new line
(setf *mynewset* (adjoin 1 *mynewset*));;*mynewset*isnow(1)
(setf *mynewset* (adjoin 2 *mynewset*));;*mynewset*isnow(1 2)
;adding an existing value
;no duplicate value
(write *mynewset*);;(1 2)
(terpri);;prints a new line
;pushing a new line
(pushnew 2 *mynewset*);;*mynewset*isnow(1 2)
(write *mynewset*);;(1 2)
(terpri);;prints a new line
(Pushnew 3 *mynewset*);;*mynewset*isnow(1 2 3)
(write *mynewset*);;(1 2 3)
(terpri);;prints a new line
Output:
NIL
(2 1)
(2 1)
(3 2 1)
B) Implementation of Reversing a given List in LISP
Aim: To implement Reversing a given list in LISP
Source code:
(defun list-append(L1 L2)
“Append L1 by L2.”
(if(null L1)
L2
(cons(first L1)(List-append(rest L1)L2))))
(defun show-list-reverse(L)
"create anew list contaning the elements of L in reversed order"
(if(null L)
nil
(list-append(show-list-reverse(restL))
(list(first L)))))
Output:
(show-list-reverse '(1 2 3))
(3 2 1)

(show-list-reverse '(1 2 3) '(4 5))


(3 2 1 4 5)
Exp.no 10: Program to implement Simulated Annealing using JAVA
Aim: To write a program to implement Simulated Annealing using JAVA
Source code:
import java.util.*;
public class StimulatedAnnealing {
public static double T=1;
static final double Tmin=0.0001;
static final double alpha=0.9;
static final int numIterations=100;
static final int M=5,N=5;
static final int k=5;
public static void main(String args[]) {
String[][] sourceArray=new String[M][N];
Solution min=new Solution(Double.MAX_VALUE,null);
Solution currentSol=genRandSol();
while(T>Tmin) {
for(int i=0; i<numIterations; i++) {
if(currentSol.CVRMSE<min.CVRMSE) {
min=currentSol;
}
Solution newSol=neighbor(currentSol);
double ap=Math.pow(Math.E,(currentSol.CVRMSE-newSol.CVRMSE)/T);
if(ap>Math.random()) {
currentSol=newSol;
}
}
T*=alpha;
}
System.out.println(min.CVRMSE+"\n\n");
for(String[] row:sourceArray)Arrays.fill(row,"X");
for(int object:min.config) {
int[] coord=indexToPoints(object);
sourceArray[coord[0]][coord[1]]="-";
}
for(String[] row:sourceArray) {
System.out.println(Arrays.toString(row));
}
}
public static Solution neighbor(Solution currentSol) {
return currentSol;
}
public static Solution genRandSol() {
int[] a={1,2,3,4,5};
return new Solution(-1,a);
}
public static double cost(int[] inputConfiguration) {
return -1;
}
public static int[] indexToPoints(int index) {
int[] points={(index%M),(index%N)};
return points;
}
static class Solution{
public double CVRMSE;
public int[] config;
public Solution(double CVRMSE,int[] Configuration) {
this.CVRMSE=CVRMSE;
config=Configuration;
}
}
}
Output:
-1.0
[-, X, X, X, X]
[X, -, X, X, X]
[X, X, -, X, X]
[X, X, X, -, X]
[X, X, X, X, -]
Exp.no 11: Implementation of Expert System with Backward chaining using PROLOG
Aim: To implement of Expert System with backward chaining using PROLOG
Source code:
#Program with three facts and one rule.
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X):-
rainy(X),
cold(X).

Output:
snowy(rochester).
true.

snowy(seattle).
false.
Exp.no 12: Program to findout Union and Intersection of two lists
Aim: To write a program to find out Union and Intersection of two lists
Algorithm:
Algorithm to Find out Union of Two Lists
Step 1: Start
Step 2: If first list is empty return second list as the union of two lists and goto step 6.
Step 3: If head of first list is not a member of second list add this element in the union of both list produced by
calling union function with and goto step 5.
Step 4: If head of first list is a member of second list call the union function for tail of first list and second list
Step 5: Exit
Algorithm to Find out Intersection of Two Lists
Step 1: Start
Step 2: If first list is empty return empty list as intersection of both lists.
Step 3: If head of first list is a member of second list then add the head to the list obtained by calling
intersection function with tail of first list and second list and goto step 5.
Step 4: If head of first list is not a member of second list then call intersection function with tail of first list
and second list.
Step 5: Exit
Source code:
domains
X
=
s
y
m
b
o
l
*
Y
=
s
y
m
b
o
l
predicates union(X,X,X)
inters(X,X,X)member(Y,X)
clauses union([],L,L).
union([H|T],L,[H|U]):-
union(T,L,U),
not(member(H,U)),!.
union([_|T],L,U):-
union(T,L,U).
inters([],_,[]).
inters([H|T],L,[H|Lr]):-
member(H,L),
inters(T,L,Lr),!.
inters([H|T],L,Lr):-
not(member(H,L)),
inters(T,L,Lr).
member(H,[H|_]).
member(H,[_,L]):-
member(H,L).
Output:
Goal: union([a,b,c],[c,d,e],X)
X=[“a”,”b”,”c”,”d”,”e”]
1 solution
Goal:
Intersection([a,b,c],[c,d,e],X)
X=[“c”]
1 solution
Exp.no 13: Implementation of Factorial and Fibonacci of a given number
Aim: To write a program to implement the Factorial and Fibonacci of a given number
Algorithm:
Algorithm to Calculate Factorial of a given Number
Step 1: Start
Step 2: If the number is 0 then return 1 and goto step 5.
Step 3: Call the function with a number one less than the original number.
Step 4: Return product of number received in step 3 and number itself as factorial.
Step 5: Exit

Factorial Source code:


factorial(0,1).
factorial(N,F):-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N*F1.
Output:
factorial(4,X).
X=24

Fibonacci Source code:


fibonacci:
fib(0,0).
fib(X,Y):-X>0,fib(X,Y,_).
fib(1,1,0).
fib(X1,Y1,Y2):-
X>1,
X is X-1,
fib(X1,Y2,Y3),
Y1 is Y2+Y3.
Output:
Fib(10,X).
X=55 .

You might also like