// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample calculates the factorial of a number.

// Examples ------------------------------------------------------------------------------------------------------------------------------

?- #factorial(3,:w)
-> ( 6 ) := 1.00 (0.003) 1
 
// Code ----------------------------------------------------------------------------------------------------------------------------------

factorial {

    (0,1)^              :- true;
    (:N ?[gt(0)],:F)    :- sub(:N,1,:N1), #factorial(:N1,:F1), mul(:N,:F1,:F);

    (0,:F,:F)^          :- true;
    (:N ?[gt(0)],:A,:F) :- mul(:N,:A,:A1), sub(:N,1,:N1), #factorial(:N1,:A1,:F);

}

// ---------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]