fork download
  1. #include <iostream>
  2. #include <assert.h>
  3.  
  4. const int terms = 10;
  5. constexpr int factorial(int n)
  6. {
  7. return n <= 1 ? 1 : (n * factorial(n-1));
  8. }
  9. constexpr double pow(double x,int n){
  10. return n==0? 1 : (n==1? x : x*pow(x,n-1));
  11. }
  12.  
  13. constexpr double exp(double x,int n){
  14. return n == terms ? pow(x,n)/factorial(n): pow(x,n)/factorial(n)+ exp(x,n+1);
  15. }
  16.  
  17. using namespace std;
  18. static const double alpha = 3.0;
  19. template <int N>
  20. struct Table {
  21. Table<N - 1> rest;
  22. const double x;
  23. Table() : x(exp(N*alpha,0)) {}
  24.  
  25. double operator[](size_t i){
  26. assert(i < N);
  27. return (reinterpret_cast<double*>(this))[i];
  28. }
  29. };
  30.  
  31. template <>
  32. struct Table<0> {
  33. const double x;
  34. Table() : x(1) {}
  35. double operator[](size_t i){
  36. assert(i < 1);
  37. return (reinterpret_cast<double*>(this))[i];
  38. }
  39. };
  40.  
  41. int main() {
  42. Table<10> t;
  43. for(int i=0;i<10;i++)
  44. cout << t[i] << endl;
  45. return 0;
  46. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
1
20.0797
386.234
5720.68
56513.3
387262
1.99385e+06
8.24382e+06
2.87407e+07
8.75158e+07