fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. double iterate(double x, double eps)
  7. {
  8. x = x*x*x;
  9. double term = x/2, sum = x/2;
  10. for(int k = 3; abs(term) > eps; ++k)
  11. sum += (term *= x/k);
  12. return sum;
  13. }
  14.  
  15. double recursion(double x, double eps, double term = 1, int k = 2)
  16. {
  17. if (abs(term) < eps) return 0;
  18. term *= x*x*x/k;
  19. return term + recursion(x,eps,term,k+1);
  20. }
  21.  
  22. int main()
  23. {
  24. for(double x = 0; x < 2; x+= 0.2)
  25. cout << setw(10) << x << setw(15) << iterate(x,1e-8) << setw(15) << recursion(x,1e-8) << endl;
  26. }
  27.  
Success #stdin #stdout 0s 4988KB
stdin
Standard input is empty
stdout
         0              0              0
       0.2     0.00401069     0.00401069
       0.4      0.0326937      0.0326937
       0.6       0.116215       0.116215
       0.8       0.305908       0.305908
         1       0.718282       0.718282
       1.2        1.67904        1.67904
       1.4        4.30213        4.30213
       1.6        13.4286        13.4286
       1.8        57.3059        57.3059
         2        371.495        371.495