Newton法パネェ
#include <stdio.h> int main() { double a, x; int count, i; printf("aの3乗根を求めます。\na:"); scanf("%lf", &a); printf("x0:"); scanf("%lf", &x); printf("反復回数:"); scanf("%d", &count); for(i = 1; i <= count; i++) { x = (2 * x + a / (x * x)) / 3; printf("x%d = %.20f (x%d^3 = %.20f)\n", i, x, i, x*x*x); } return 0; }
aの3乗根を求めます。 a:10 x0:10 反復回数:10 x1 = 6.70000000000000017800 (x1^3 = 300.76300000000003360000) x2 = 4.54092225439964369800 (x2^3 = 93.63370300166980087000) x3 = 3.18893705034208307900 (x3^3 = 32.42931976553633433000) x4 = 2.45374135571122797800 (x4^3 = 14.77360039874827358000) x5 = 2.18945956481615100500 (x5^3 = 10.49568497518751542000) x6 = 2.15499199671155583500 (x6^3 = 10.00776237299924886800) x7 = 2.15443483414562608800 (x7^3 = 10.00000200675034633900) x8 = 2.15443469003189314000 (x8^3 = 10.00000000000013145000) x9 = 2.15443469003188381400 (x9^3 = 10.00000000000000177600) x10 = 2.15443469003188381400 (x10^3 = 10.00000000000000177600)
適当な初期値でも9回で収束した、だと…!? まさかこれほどとは… 大した奴だ。