-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpower.pl
More file actions
28 lines (21 loc) · 1.65 KB
/
Copy pathpower.pl
File metadata and controls
28 lines (21 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%****************************************************************************************************
% *
% Calculate the Power of a Number A to the Power of B *
% *
% The following Prolog code defines predicates `power/3` to calculate A raised to the power B. *
% *
% - `power/3`: Entry point to calculate A raised to the power B. *
% *
% Example usage: *
% ?- power(2, 5, Result). *
% Result = 32 *
% *
%****************************************************************************************************
% ------------------------------------Base case: Any number A raised to power 0 is 1----------------------------------------
power(_, 0, 1).
% ----------------------------------------Calculate A raised to the power B----------------------------------------
power(A, B, Result) :-
B > 0,
B1 is B - 1,
power(A, B1, R1),
Result is A * R1.