Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
906 B
1
Indexable
Never
#include <stdio.h>
#include <math.h>

double directSum(int N) {
    double sum = 0.0;
    for (int n = 1; n <= N; n++) {
        sum += sqrt((1 + 2 * n) / (3.0 + n));
    }
    return sum;
}

double cohenSum(int N) {
    double S = 0.0;
    double cor = 0.0;

    for (int n = 1; n <= N; n++) {
        double f = sqrt((1 + 2 * n) / (3.0 + n));
        double fcor = f - cor;
        double Scor = S + fcor;
        cor = (Scor - S) - fcor;
        S = Scor;
    }

    return S;
}

int main() {
    for (int N = 20000000; N <= 200000000; N += 20000000) {
        double directResult = directSum(N);
        double cohenResult = cohenSum(N);
        double difference = directResult - cohenResult;

        // Output the table with the specified formatting
        printf("%9d%18.4lf%18.4lf%10.4lf\n", N, directResult, cohenResult, difference);
    }

    return 0;
}