fork download
  1. // Excluding not applicable arguments
  2.  
  3. const fact1 = function (n) {
  4. function f(n) {
  5. if (n == 0) return 1;
  6. return n * f(n - 1);
  7. }
  8. if (n < 0) return -1; // not applicable
  9. return f(n);
  10. };
  11.  
  12. console.log("Factorial:");
  13. for (let i = -3; i <= 16; i++) {
  14. console.log(`${i}!=${fact1(i)}`);
  15. }
  16.  
  17. const fib1 = function (n) {
  18. function f(n) {
  19. if (n <= 1) return n;
  20. return f(n - 1) + f(n - 2);
  21. }
  22. if (n < 0) return -1; // not applicable
  23. return f(n);
  24. };
  25.  
  26. console.log("Fibonacci sequence:");
  27. for (let i = -3; i <= 20; i++) {
  28. console.log(`f(${i})=${fib1(i)}`);
  29. }
Success #stdin #stdout 0.05s 18872KB
stdin
Standard input is empty
stdout
Factorial:
-3!=-1
-2!=-1
-1!=-1
0!=1
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
11!=39916800
12!=479001600
13!=6227020800
14!=87178291200
15!=1307674368000
16!=20922789888000
Fibonacci sequence:
f(-3)=-1
f(-2)=-1
f(-1)=-1
f(0)=0
f(1)=1
f(2)=1
f(3)=2
f(4)=3
f(5)=5
f(6)=8
f(7)=13
f(8)=21
f(9)=34
f(10)=55
f(11)=89
f(12)=144
f(13)=233
f(14)=377
f(15)=610
f(16)=987
f(17)=1597
f(18)=2584
f(19)=4181
f(20)=6765