From 4d1c3d9ab20c27f4c628d437623ec70166b65708 Mon Sep 17 00:00:00 2001 From: Sven Vogel Date: Mon, 12 Jun 2023 15:19:03 +0000 Subject: [PATCH] added file containing predicates for various math functions --- Math.pro | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Math.pro diff --git a/Math.pro b/Math.pro new file mode 100644 index 0000000..cd0531e --- /dev/null +++ b/Math.pro @@ -0,0 +1,31 @@ +% Compute the nth number in the fibonacci sequence +fib(0, 0). +fib(1, 1). +fib(X, Res):- + X1 is X - 1, + X2 is X - 2, + fib(X1, Tmp1), + fib(X2, Tmp2), + Res is Tmp1 + Tmp2. + +% Compute the nth element of the integer factorial +fac(0, 1). +fac(X, Res):- + X1 is X - 1, + fac(X1, Tmp), + Res is Tmp * X. + +% Sums all integer numbers between and inclusive X and inclusive Y and writes the result to Res. +% +% example: sum(0, 9, X). +% output: X = 45 +% explanation: 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 9 +sum(X, X, X). +sum(X, Y, Res):- + X < Y, + X1 is X + 1, + sum(X1, Y, Tmp), + Res is Tmp + X. +sum(X, Y, Res):- % if X is bigger than Y, swap the operands and call sum again + X > Y, + sum(Y, X, Res). \ No newline at end of file