#include <stdio.h>
#include <math.h>
// 方程式 f(x) を定義
double f(double x) {
return pow(x
, 3) - 7.0; // f(x) = x^4 - 7 }
// 二分法の実装
double nibun(double a, double b, double gosa) {
double m;
while (fabs(b
- a
) > gosa
) { m = (a + b) / 2.0;
if (fabs(f
(m
)) < gosa
) { // f(m) == 0 ではなく、誤差を考慮 break;
} else if (f(a) * f(m) < 0.0) {
b = m;
} else {
a = m;
}
}
return m;
}
int main() {
double a = 1.5, b = 2.0, gosa = 1e-8; // 初期区間と許容誤差
// 二分法で解を計算
double root = nibun(a, b, gosa);
// 結果を表示
printf("7^(1/3) の近似解: %.8lf\n", root
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgovLyDmlrnnqIvlvI8gZih4KSDjgpLlrprnvqkKZG91YmxlIGYoZG91YmxlIHgpIHsKICAgIHJldHVybiBwb3coeCwgMykgLSA3LjA7IC8vIGYoeCkgPSB4XjQgLSA3Cn0KCi8vIOS6jOWIhuazleOBruWun+ijhQpkb3VibGUgbmlidW4oZG91YmxlIGEsIGRvdWJsZSBiLCBkb3VibGUgZ29zYSkgewogICAgZG91YmxlIG07CiAgICB3aGlsZSAoZmFicyhiIC0gYSkgPiBnb3NhKSB7CiAgICAgICAgbSA9IChhICsgYikgLyAyLjA7CiAgICAgICAgaWYgKGZhYnMoZihtKSkgPCBnb3NhKSB7IC8vIGYobSkgPT0gMCDjgafjga/jgarjgY/jgIHoqqTlt67jgpLogIPmha4KICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfSBlbHNlIGlmIChmKGEpICogZihtKSA8IDAuMCkgewogICAgICAgICAgICBiID0gbTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBhID0gbTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gbTsKfQoKaW50IG1haW4oKSB7CiAgICBkb3VibGUgYSA9IDEuNSwgYiA9IDIuMCwgZ29zYSA9IDFlLTg7IC8vIOWIneacn+WMuumWk+OBqOioseWuueiqpOW3rgoKICAgIC8vIOS6jOWIhuazleOBp+ino+OCkuioiOeulwogICAgZG91YmxlIHJvb3QgPSBuaWJ1bihhLCBiLCBnb3NhKTsKCiAgICAvLyDntZDmnpzjgpLooajnpLoKICAgIHByaW50ZigiN14oMS8zKSDjga7ov5HkvLzop6M6ICUuOGxmXG4iLCByb290KTsKCiAgICByZXR1cm4gMDsKfQoK