# Archive for category Prolog

### Prolog code for maximum, minimum, oddeven, permutation and nth element

Posted by Bikash Karmokar in Prolog on August 2, 2011

**Maximum number:**

max_list([H|T],M):-max_list(T,H,M).

max_list([],C,C).

max_list([H|T],C,M):-

C2 is max(C,H),

max_list(T,C2,M).

**Input and output:**

%max_list([4,5,1,9],M).

%M=9

**Minimum number:**

minl([Only], Only).

minl([Head|Tail], Minimum) :-

minl(Tail, TailMin),

Minimum is min(Head, TailMin).

Input = minl([4,5,1,2,9],X).

**Odd or even:**

odd_even(X):- X mod 2 =:= 0,

write(‘even number’),nl.

odd_even(X):- write(‘odd number’),nl.

**Permutation:**

perm(List,[H|Perm]):-delete(H,List,Rest),perm(Rest,Perm).

perm([],[]).

delete(X,[X|T],T).

delete(X,[H|T],[H|NT]):-delete(X,T,NT).

**Input:**

perm([1,2,3],B).

**Nth element:**

nth(Count, Item, [_|Tail]):-

Count > 1,

Count0 is Count-1,

nth(Count0, Item, Tail).

nth(1,Head,[Head|_]).

%nth(pos,num.list).

%pos=2

%num=3

%ache kina chke kre yes or no rslt

%nth(2,3,[1,3,5,6]).

%Yes

%nth(2,3,[1,1,5,6]).

%No

### Prolog code for fibonacci series and number

Posted by Bikash Karmokar in Prolog on August 2, 2011

**Finding fibonacci series:**

:- dynamic(stored/1).

memo(Goal) :-

stored(Goal) -> true;

Goal, assertz(stored(Goal)).

fib(1,1) :- !, write(‘1, ‘).

fib(2,1) :- !, write(‘1, ‘).

fib(N,F) :-

N1 is N-1, memo(fib(N1,F1)),

N2 is N-2, memo(fib(N2,F2)),

F is F1 + F2,

write(F), write(‘, ‘).

Input = fib(7,X).

Output: 1, 1, 2, 3, 5, 8, 13

**Other fibonacci code:**

fib(0,0).

fib(1,1).

fib(X, Ans) :- Xm1 is X – 1 ,Xm2 is X – 2 ,

fib(Xm1, Sub1), fib(Xm2, Sub2),

Ans is Sub1 + Sub2.

fib(0,A). means A=0; 0 tomo fibonacci number is 0

fib(7,A). A=13; 7 th fibonacci number is 13

fibonacci series: 0,1,1,2,3,5,8,13,21,34,55,89,144;

**another one**:

fib_for(N,_,N).

fib_for(N1,N2,N):-

N3 is N1+N2,

fib_for(N2,N3,N).

%fib_for(FirstNumber, SecondNumber, Result).

% Example: ?-fib_for(1,1,Result).

%at first have to provide two fibonacci number then have to start execution, when executing have to press ; for next fibonacci number.

### Prolog code for prime number, power calculation and factorial

Posted by Bikash Karmokar in Prolog on August 2, 2011

**Checking prime number:**

divisible(X,Y):-

N is Y*Y,

N =< X,

X mod Y =:= 0.

divisible(X,Y):-

Y < X,

Y1 is Y+1,

divisible(X,Y1).

isprime(X):-

Y is 2, X > 1, \+divisible(X,Y).

Input: isprime(7).

Output: Yes

**Finding power of a number:**

power(0,N,0):- N>0.

power(X,0,1):- X>0.

power(X,N,V):-X>0,N>0,N1 is N-1,power(X,N1,V1), V is V1*X.

Input: power(2,4,P).

Output: V=16

**Finding factorial of a number:**

fact(0,1).

fact(X,Y):-

X1 is X-1,

fact(X1,Z),

Y is X*Z.

Input: fact(3,X).

Output: X=6

### Prolog code for GCD and LCM

Posted by Bikash Karmokar in Prolog on August 2, 2011

**Find the GCD of the two number X and Y.**

**gcd(X, Y, G) :- X = Y, G = X. **

**gcd(X, Y, G) :- **

** X < Y, **

** Y1 is Y – X, **

** gcd(X, Y1, G). **

**gcd(X, Y, G) :- X > Y, gcd(Y, X, G). **

**Input: gcd(10,5,G). **

**Output: G = 5**

**Find the LCM of the two number X and Y.**

**gcd(X, Y, G) :- X = Y, G = X. **

**gcd(X, Y, G) :- **

** X < Y, **

** Y1 is Y – X, **

** gcd(X, Y1, G). **

**gcd(X, Y, G) :- X > Y, gcd(Y, X, G). **

**lcm(X,Y,LCM):-gcd(X,Y,GCD), LCM is X*Y//GCD. **

**Input: lcm(4,5,M). **

**Output: M = 20**