#include <stdio.h>
#include <math.h> // pow関数を使用するため
double x(int n) {
double sum = 1.0; // 最初の項は1
int i;
for (i = 1; i < n; i++) { // forループ内の変数宣言を外へ
sum
+= pow(-1, i
) / (2 * i
+ 1); // (-1)^i / (2i + 1) の計算 }
return 4 * sum; // 4倍して結果を返す
}
int main() {
int n1 = 3, n2 = 10;
printf("x(%d) = %lf\n", n1
, x
(n1
)); printf("x(%d) = %lf\n", n2
, x
(n2
)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+IC8vIHBvd+mWouaVsOOCkuS9v+eUqOOBmeOCi+OBn+OCgQoKZG91YmxlIHgoaW50IG4pIHsKICAgIGRvdWJsZSBzdW0gPSAxLjA7IC8vIOacgOWIneOBrumgheOBrzEKICAgIGludCBpOwogICAgZm9yIChpID0gMTsgaSA8IG47IGkrKykgeyAvLyBmb3Ljg6vjg7zjg5flhoXjga7lpInmlbDlrqPoqIDjgpLlpJbjgbgKICAgICAgICBzdW0gKz0gcG93KC0xLCBpKSAvICgyICogaSArIDEpOyAvLyAoLTEpXmkgLyAoMmkgKyAxKSDjga7oqIjnrpcKICAgIH0KICAgIHJldHVybiA0ICogc3VtOyAvLyA05YCN44GX44Gm57WQ5p6c44KS6L+U44GZCn0KCmludCBtYWluKCkgewogICAgaW50IG4xID0gMywgbjIgPSAxMDsKICAgIHByaW50ZigieCglZCkgPSAlbGZcbiIsIG4xLCB4KG4xKSk7CiAgICBwcmludGYoIngoJWQpID0gJWxmXG4iLCBuMiwgeChuMikpOwogICAgcmV0dXJuIDA7Cn0K