fork download
  1. #include <cstdio>
  2. #include <limits>
  3.  
  4. #include <cstdint>
  5. #include <vector>
  6. #include <string>
  7. #include <cstdio>
  8. #include <iostream>
  9.  
  10. unsigned long factorial1(unsigned long(*self)(unsigned long), unsigned long n) {
  11. return n ? n * self(n - 1) : 1;
  12. }
  13.  
  14. template<class X, X(*Fn)(X(*)(X), X)>
  15. X Function(X x) {
  16. return Fn(Function<X, Fn>, x);
  17. }
  18.  
  19. unsigned long factorial(unsigned long n) {
  20. return Function<unsigned long, factorial1>(n);
  21. };
  22.  
  23. int main() {
  24. std::cout << factorial(1) << '\n';
  25. std::cout << factorial(2) << '\n';
  26. std::cout << factorial(3) << '\n';
  27. std::cout << factorial(4) << '\n';
  28. std::cout << factorial(5) << '\n';
  29. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1
2
6
24
120