Hiren Al 2
Hiren Al 2
2
Name: Hiren Daxeshbhai Patel Roll No.: 07
Objective: Implementation of Factorial, Fibonacci series and Prime Number Checking in SWI
Prolog.
Resources used:
• Windows 10,11
• PC i5 or greater
• Software SWI Prolog
Theory:
1. Factorial:
• The factorial of a whole number is the function that multiplies the number by every
natural number below it.
• Prolog Factorial function definition is also similar to a normal factorial function.
• Factorial(0,1) i.e., factorial of 0 is generally 1.
• Factorial(N,M), if any temporary value N1 is assigned to N-1.
• Factorial(N1,M1), and is factorial of N1 is M1.
• M is NM1 i.e., assigning M to N*M1, then value of N is M.
• The above happens to be the recursive relation between N and factorial M. It reviews
rules for particular relation in the top to bottom order.
2. Fibonacci Series:
• The Fibonacci series, named after Italian mathematician named Leonardo Pisano
Bogollo, later known as Fibonacci, is a series (sum) formed by Fibonacci numbers
denoted as Fn. The fibonacci series numbers are given as: 0, 1, 1, 2, 3, 5, 8, 13, 21,
38, . . . In a Fibonacci series, every term is the sum of the preceding two terms,
starting from 0 and 1 as the first and second terms. In some old references, the term
'0' might be omitted. The series has captured the interestof mathematicians and it
continues to be studied and explored for its captivating properties.
• The Fibonacci series is the sequence of numbers (also called Fibonacci numbers),
where every number is the sum of the preceding two numbers, such that the first two
terms are '0' and '1'.
• Formula of Fibonacci series is, Fn = Fn-1 + Fn-2.
Fo = 0, F1=1
Fn = Fn-1 + Fn-2
For >1
• Fibonacci series : 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
3. Prime Numbers:
• Prime numbers are numbers that have only 2 factors: 1 and themselves. For example,
the first 5 prime numbers are 2, 3, 5, 7, and 11.
• Module prime provides predicates to test (positive integer) numbers for primality,
find divisors and factor numbers, generate prime numbers in some interval, find
consecutive prime numbers, and save/load all prime numbers up to some value
to/from a file or stream.
• All predicates in module prime are safe, i.e. validate input arguments and ensure
steadfastness. For maximum performance, user code can directly call the unsafe
public (not exported) predicates in module prime_lgc.
• Implements a variant of the Miller-Rabin primality test that is deterministic for
numbers up to 3317044064679887385961980, otherwise it is probabilistic with the
number of iterations fixed at 20.
• For better performance, leverages a prime wheel of level 4, i.e. generated by the first
4 consecutive prime numbers, and the memoization of pairs of consecutive prime
numbers.
Source Code:
1. Factorial:
factorial(0,1).
factorial(N,M):-
N>0,
N1 is N-1,
factorial(N1,M1),
M is N*M1.
Output:
2. Fibonacci Series:
fib(0, 1) :- !. fib(1,
1) :- !.
fib(N, F) :-
N > 1,
N1 is N-1,
N2 is N-2,
fib(N1, F1),
fib(N2, F2),
F is F1+F2.
Output:
3. 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).
Output:
Conclusions:
Successfully implemented the Factorial, Fibonacci Series and Prime Number Checking program
in SWI Prolog.