Website Summary Files Commits Branches @commit @diff @stats
path: root / Week05 / Exercise19.cpp
blob: baad3c78fe7d276632f245e311b474e9d39afba7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

int main()
{
    double x;
    unsigned int n;
    std::cin >> x >> n;

    double sum = 0;

    for (unsigned int k = 0; k <= n; k++) {
        unsigned int l = 2 * k + 1;

        double to_power = 1;
        for (unsigned p = 1; p <= l; p++) {
            to_power *= x;
        }

        unsigned int fac = 1;
        for (unsigned i = 1; i <= l; i++) {
            fac *= i;
        }

        sum += (k % 2 ? -1.0 : 1.0) * to_power / fac;
    }

    std::cout << sum << std::endl;

    return 0;
}