[ create a new paste ] login | about

Link: http://codepad.org/KsvWjolN    [ raw code | output | fork ]

salvador@conclase.net - C++, pasted on Nov 13:
// Suma de la serie 1-1/2+1/3-1/4+1/5...
// (C) 2009 Con Clase
// Salvador Pozo

#include <iostream>

using namespace std;

double par(int);
double impar(int);
double suma(int);

int main() {

    cout << suma(3) << endl;
    cout << suma(13) << endl;
    cout << suma(23) << endl;
    cout << suma(87) << endl;
    cout << suma(250) << endl;
    cout << suma(450) << endl;
    return 0;
}

double suma(int n) {
    if(n % 2) return impar(n);
    else return par(n);
}

double par(int n) {
    return impar(n-1)-1/double(n);
}

double impar(int n) {
    if(n == 1) return 1;
    return par(n-1)+1/double(n);
}


Output:
1
2
3
4
5
6
0.833333
0.730134
0.714414
0.698861
0.691151
0.692037


Create a new paste based on this one


Comments: