#include <stdio.h>
#include <math.h>
double newton_method_cubic(double x0, int max_iter, double tol) {
double x = x0;
for (int i = 0; i < max_iter; i++) {
double fx = x * x * x - 7;
double dfx = 3 * x * x;
double x_next = x - fx / dfx;
if (fabs(x_next
- x
) < tol
) { return x_next;
}
x = x_next;
}
return x;
}
int main() {
double initial_guess = 2.0; // 初期値
int max_iterations = 100; // 最大反復回数
double tolerance = 1e-7; // 許容誤差
double root = newton_method_cubic(initial_guess, max_iterations, tolerance);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+Cgpkb3VibGUgbmV3dG9uX21ldGhvZF9jdWJpYyhkb3VibGUgeDAsIGludCBtYXhfaXRlciwgZG91YmxlIHRvbCkgewogICAgZG91YmxlIHggPSB4MDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbWF4X2l0ZXI7IGkrKykgewogICAgICAgIGRvdWJsZSBmeCA9IHggKiB4ICogeCAtIDc7CiAgICAgICAgZG91YmxlIGRmeCA9IDMgKiB4ICogeDsKICAgICAgICBkb3VibGUgeF9uZXh0ID0geCAtIGZ4IC8gZGZ4OwogICAgICAgIGlmIChmYWJzKHhfbmV4dCAtIHgpIDwgdG9sKSB7CiAgICAgICAgICAgIHJldHVybiB4X25leHQ7CiAgICAgICAgfQogICAgICAgIHggPSB4X25leHQ7CiAgICB9CiAgICByZXR1cm4geDsKfQoKaW50IG1haW4oKSB7CiAgICBkb3VibGUgaW5pdGlhbF9ndWVzcyA9IDIuMDsgLy8g5Yid5pyf5YCkCiAgICBpbnQgbWF4X2l0ZXJhdGlvbnMgPSAxMDA7IC8vIOacgOWkp+WPjeW+qeWbnuaVsAogICAgZG91YmxlIHRvbGVyYW5jZSA9IDFlLTc7IC8vIOioseWuueiqpOW3rgoKICAgIGRvdWJsZSByb290ID0gbmV3dG9uX21ldGhvZF9jdWJpYyhpbml0aWFsX2d1ZXNzLCBtYXhfaXRlcmF0aW9ucywgdG9sZXJhbmNlKTsKICAgIHByaW50Zigi4oibNyDiiYggJS4xMGZcbiIsIHJvb3QpOwoKICAgIHJldHVybiAwOwp9