Lab Report factorial tail recursion
Lab Report factorial tail recursion
Objectives:
Introduction:
In this lab, we examine how to compute the factorial of a number using Prolog. The factorial
operation involves multiplying a number by all positive integers less than itself. For instance, the
factorial of 5 (written as 5!) is 5 * 4 * 3 * 2 * 1, which equals 120. We will create a Prolog predicate
to calculate the factorial using recursion. The implementation includes a base case for the factorial
of 0 and a recursive case for positive integers.
Code Implementation:
Factorial (version 1)
%fact
factorial(0,1).
%rule
factorial(N,F):-
N>0,
N1 is N-1,
factorial(N1,F1),
F is F1 * N.
fact(N, 1, F).
fact(N, Acc, F) :-
N > 0,
NewAcc is N * Acc,
NewN is N - 1,
Output:
Code Explanation:
The given Prolog code calculates the factorial of a number using these predicates:
fact(N, F):
When N is 0, the accumulator (Acc) holds the final result, which is the factorial of the original
number.
If N is greater than 0, it calculates the new accumulator value (NewAcc) by multiplying the current
Acc with N.
Conclusion:
This lab demonstrated how to compute the factorial of a number using Prolog. We established a
base case for when the number is 0 and a recursive case for positive numbers. Testing the
implementation with various values confirmed its correctness. Prolog’s support for recursion
makes it well-suited for this task, and the organized code structure ensures the calculation is both
straightforward and efficient.
Lab Report: Fibonacci using tail Recursion
Objectives:
Introduction:
In this lab, we focus on computing Fibonacci numbers using Prolog. The Fibonacci sequence is a
series in which each number is the sum of the two previous numbers, starting from 0 and 1. For
instance, the sequence begins as 0, 1, 1, 2, 3, 5, 8, and so forth.
Code Implementation:
fibo(N,F):-
fibo_helper(N,0,1,F).
fibo_helper(0,Acc, _, Acc).
N>0,
N1 is N-1,
fibo_helper(N1,Curr,Next,Res).
Output:
Code Explanation:
fibo(N, F): Initiates the Fibonacci calculation by calling fibo_helper/4 with starting values.
fibo_helper(0, Acc, _, Acc): Base case; when N is 0, the accumulator (Acc) holds the
result.
fibo_helper(N, Prev, Curr, Res): Recursive case; calculates the next Fibonacci number
by adding Prev and Curr, then recurses with updated values until N is 0.
Conclusion:
This lab demonstrated how to compute Fibonacci numbers using Prolog. The implementation
includes a base case for when N is 0 and a recursive case for positive values. Testing the code with
different inputs confirmed its correctness. Prolog's use of recursion effectively manages Fibonacci
calculations, and the clear code structure enhances readability and understanding.
Lab Report: Logic Gates Implementation
Objectives:
Introduction:
In this lab, we examine how to model basic logic gates using Prolog. Logic gates are crucial elements in
digital circuits and computing systems. We concentrate on implementing three primary types of gates:
AND Gate: Produces true only when both inputs are true.
OR Gate: Produces true if at least one of the inputs is true.
NOT Gate: Outputs the inverse of the input.
XOR Gate: Outputs true if exactly one of the inputs is true.
We will create Prolog predicates to represent these gates' behavior and test them with different inputs to
verify their accuracy.
Code Implementation:
AND Gate:
and_gate(X1,X2,Output):-
W1 is 1,
W2 is 1,
Theta is 2,
Sum is X1 * W1 + X2 * W2,
Output:
OR Gate:
% OR_gate
W1 is 1,
W2 is 1,
Theta is 1,
Sum is X1 * W1 + X2 * W2,
Output:
NOT Gate:
% NOT_gate
not_gate(X, Output) :-
W is -1,
Theta is 0,
Sum is X * W,
Output:
XOR Gate:
% XOR gate
xor_gate(X1, X2, Output) :-
not_gate(AndOutput, NotAndOutput),
Output:
Conclusion:
This lab showcased the implementation of fundamental logic gates using Prolog. We modeled the AND,
OR, NOT, and XOR gates by defining predicates and tested them with different input values. The
implementation correctly mirrors the behavior of these gates, and the organized code structure makes the
logic straightforward and easy to comprehend.
Lab Report: Creating Family tree
Objectives:
Introduction:
Prolog is a programming language specialized for knowledge representation and logical reasoning. In this
lab, we use Prolog to model family relationships. By defining straightforward facts and rules, we can
determine relationships such as son, daughter, mother, father, or grandfather.
We start by establishing basic relationships using facts. Next, we define rules to derive more complex
relationships based on these initial facts. The facts provide foundational information, while the rules
enable us to uncover additional connections.
Implementation:
%fact
parent(john,bob).
parent(john,samantha).
parent(mary,bob).
parent(mary,samantha).
parent(donald,john).
male(john).
male(bob).
male(donald).
female(mary).
female(samantha).
%rule
son(X,Y):-parent(Y,X),male(X).
daugther(X,Y):- parent(Y,X),female(X).
mother(X,Y):-parent(X,Y),female(X).
Output:
Conclusion:
This lab demonstrated how to model family relationships in Prolog using facts and rules. By establishing
foundational information, such as parental relationships and gender, and creating rules to determine
more complex connections like son, daughter, mother, father, or grandfather, we can effectively analyze
and understand family structures. Prolog’s logical reasoning capabilities make it a powerful tool for this
kind of symbolic representation.