- class Factorial {
- Int factorial1(Int x) {
- return if (x == 0) 1
- else x * this.factorial1(x - 1)
- }
-
- Int factorial2_aux(Int x, Int acc) {
- Int r = 0;
- if (x == 0) {
- r = acc;
- } else {
- r = this.factorial2_aux(x - 1, acc * x);
- };
- return r
- }
-
- Int factorial2(Int x) {
- return this.factorial2_aux(x, 1)
- }
-
- Int factorial3(Int x) {
- Int p = 1;
- while (x > 0) {
- p = p * x;
- x = x - 1;
- }
- return p
- }
-
- }
-
- class Example {
- Null main() {
- Factorial fac = new Factorial();
- Int x = 5;
- printInt(fac.factorial1(x));
- printChar(10);
- printInt(fac.factorial2(x));
- printChar(10);
- printInt(fac.factorial3(x));
- printChar(10);
- }
- }
-
- new Example().main()