Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <cmath>
- using namespace std;
- double tiesioginis(int n);
- double rec1(int n);
- double rec2_start(int n);//pagalbine funkcija vartotojui, kad ivedinetu i argumentus tik 1 skaiciu
- double rec2(double &Sprev, double &aprev, int i, int n);
- double vs = 0;
- double rez;
- #define VS_ON
- int main()
- {
- clock_t t1, t2;
- for (int i = 1; i < 19; i++) {
- int kart = 1000000;
- t1 = clock();
- for (int k = 0; k < kart; k++) {
- vs = 0;
- rez = tiesioginis(i);
- }
- t2 = clock();
- cout << i << " " << rez << " " << ((float)(t2 - t1)) / (float)(CLOCKS_PER_SEC*kart) << " " << vs << endl;
- }
- //system("pause");
- return 0;
- }
- double tiesioginis(int n) {
- double a = 3. / 5.;
- double S = a;
- for (int i = 1; i <= n; i++) {
- #ifdef VS_ON
- vs += 4;
- #endif
- a = (1 / (double)i)*(pow((3. / 5.), (double)i));
- S += a;
- }
- return S;
- }
- double rec1(int n) {
- if (n < 1)
- return 3. / 5.;
- #ifdef VS_ON
- vs += 5;
- #endif
- return rec1(n - 1) + (1 / (double)n)*(pow((3. / 5.), (double)n));
- }
- double rec2_start(int n) {
- double a1 = 3. / 5.;
- double S = a1;
- return rec2(S, a1, 2, n);
- }
- double rec2(double &Sprev, double &aprev, int i, int n) {
- if (i > n)
- return Sprev;
- aprev = aprev * (((double)i - 3.) / (5.*(double)i));
- Sprev += aprev;
- #ifdef VS_ON
- vs += 5;
- #endif
- return rec2(Sprev, aprev, i + 1, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement