Factorial in C
int fact(int n) {
int sofar = 1;
while (n > 0) sofar *= n--;
return sofar;
}
Factorial in Java
public class MyInt {
private int value;
public MyInt (int value) {
this.value=value;
}
public int getValue() {
return value;
}
public MyInt getFact() {
return new MyInt(fact(value));
}
private int fact(int n) {
int sofar = 1;
while (n>1) sofar *= n--;
return sofar;
}
}
Factorial in COMMON LISP
(defun fact (x)
(if (<= x 0)
1
(* x (fact (- x 1)))
)
)
Factorial in Prolog
fact(1,1).
fact(X,Fact) :-
is X-1,
(NewX, NF),
is X * NF.