Sem 7‎ > ‎AI‎ > ‎

WAP - More Mathematical Functions.

posted Aug 6, 2013, 12:37 AM by Neil Mathew



Maths.pl

natural_number(N) :- integer(N),N>0.

add(M,N,Sum) :- Sum is M+N.
sub(M,N,Diff) :- Diff is M-N.
mul(M,N,Prod) :- Prod is M^N.
div(M,N,Sol,Rem) :- Sol is M/N, Rem is mod(M,N).
exp(M,N,Exp) :- Exp is M**N.

factorial(0,1).
factorial(N,F) :- N>0, M is N-1, factorial(M,Fm), F is N*Fm.
Queries:


11 ?- factorial(3,F).
F = 6 .

12 ?- 
|    .
ERROR: Stream user_input:5:57 Syntax error: Unexpected end of clause
12 ?- natural_number(5).
true.

13 ?- natural_number(-1).
false.

14 ?- factorial(5).
ERROR: Undefined procedure: factorial/1
ERROR:     However, there are definitions for:
ERROR:         factorial/2
false.

15 ?- factorial(5,F).
F = 120 .

16 ?- div(7,3,Sol,Rem).
Sol = 2.3333333333333335,
Rem = 1.
Now, LCM, GCD:


gcd(A,B,GCD):- A=B,GCD=A.
gcd(A,B,GCD):- A<B,NB is B-A, gcd(A,NB,GCD).
gcd(A,B,GCD):- A>B, NA is A-B,gcd(NA,B,GCD).

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

Lab work:

1. arith
2. facotorial
3. gcd/lcm
4. rerlation (family)
5. health

Comments