Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. double factorial(int arg)
  5. {
  6.     if(arg < 2)
  7.     {
  8.         return arg;
  9.     }
  10.     else
  11.     {
  12.         return arg * factorial(arg - 1);
  13.     }
  14. }
  15.  
  16. int main()
  17. {
  18.     using namespace std;
  19.  
  20.     int CountOfElements = 10;
  21.  
  22.     double eps = 0.01;
  23.     double buffForOneElem = 0;
  24.     double accumForAllElems = 0;
  25.     double delta = 1;
  26.     int n = 1;
  27.     int k = 1;
  28.     for(int k = 1; k < CountOfElements; k++)
  29.     {
  30.         buffForOneElem = 0;
  31.         n = 1;
  32.         do
  33.         {
  34.             delta = (pow(n, k) / factorial(n));
  35.             buffForOneElem += delta;
  36.             n++;
  37.         }while(delta > eps);
  38.         buffForOneElem /= M_E;
  39.         cout << k << "th element is " << round(buffForOneElem) << endl;
  40.         accumForAllElems += buffForOneElem;
  41.     }
  42.  
  43.     cout << "Sum of " << CountOfElements << " elements is " << accumForAllElems << endl;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement