Newer
Older
zweic / tests / 3 / Factorial.zwei
  1. class Factorial {
  2. Int factorial1(Int x) {
  3. return if (x == 0) 1
  4. else x * this.factorial1(x - 1)
  5. }
  6.  
  7. Int factorial2_aux(Int x, Int acc) {
  8. Int r = 0;
  9. if (x == 0) {
  10. r = acc;
  11. } else {
  12. r = this.factorial2_aux(x - 1, acc * x);
  13. };
  14. return r
  15. }
  16.  
  17. Int factorial2(Int x) {
  18. return this.factorial2_aux(x, 1)
  19. }
  20.  
  21. Int factorial3(Int x) {
  22. Int p = 1;
  23. while (x > 0) {
  24. p = p * x;
  25. x = x - 1;
  26. }
  27. return p
  28. }
  29.  
  30. }
  31.  
  32. class Example {
  33. Null main() {
  34. Factorial fac = new Factorial();
  35. Int x = 5;
  36. printInt(fac.factorial1(x));
  37. printChar(10);
  38. printInt(fac.factorial2(x));
  39. printChar(10);
  40. printInt(fac.factorial3(x));
  41. printChar(10);
  42. }
  43. }
  44.  
  45. new Example().main()