Advertisement
Guest User

Untitled

a guest
May 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <cmath>
  4. using namespace std;
  5. double tiesioginis(int n);
  6. double rec1(int n);
  7. double rec2_start(int n);//pagalbine funkcija vartotojui, kad ivedinetu i argumentus tik 1 skaiciu
  8. double rec2(double &Sprev, double &aprev, int i, int n);
  9. double vs = 0;
  10. double rez;
  11. #define VS_ON
  12. int main()
  13. {
  14.     clock_t t1, t2;
  15.     for (int i = 1; i < 19; i++) {
  16.         int kart = 1000000;
  17.         t1 = clock();
  18.         for (int k = 0; k < kart; k++) {
  19.             vs = 0;
  20.             rez = tiesioginis(i);
  21.         }
  22.         t2 = clock();
  23.         cout << i << " " << rez << " " << ((float)(t2 - t1)) / (float)(CLOCKS_PER_SEC*kart) << " " << vs << endl;
  24.     }
  25.     //system("pause");
  26.     return 0;
  27. }
  28. double tiesioginis(int n) {
  29.     double a = 3. / 5.;
  30.     double S = a;
  31.     for (int i = 1; i <= n; i++) {
  32. #ifdef VS_ON
  33.         vs += 4;
  34. #endif
  35.         a = (1 / (double)i)*(pow((3. / 5.), (double)i));
  36.         S += a;
  37.     }
  38.     return S;
  39. }
  40. double rec1(int n) {
  41.     if (n < 1)
  42.         return 3. / 5.;
  43. #ifdef VS_ON
  44.     vs += 5;
  45. #endif
  46.     return rec1(n - 1) + (1 / (double)n)*(pow((3. / 5.), (double)n));
  47. }
  48. double rec2_start(int n) {
  49.     double a1 = 3. / 5.;
  50.     double S = a1;
  51.     return rec2(S, a1, 2, n);
  52. }
  53. double rec2(double &Sprev, double &aprev, int i, int n) {
  54.     if (i > n)
  55.         return Sprev;
  56.     aprev = aprev * (((double)i - 3.) / (5.*(double)i));
  57.     Sprev += aprev;
  58. #ifdef VS_ON
  59.     vs += 5;
  60. #endif
  61.     return rec2(Sprev, aprev, i + 1, n);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement