#include <iostream> #include <math.h> using namespace std; double Grial(int x) { double result = 1; double num = x; do{ result = num; num = result/2.0+x/2.0/result; }while(fabs(num-result)>0.00001); return result; } //牛顿迭代公式x(n+1)=x(n)-f(x(n))/f'(x(n)) //求N的平方根就是求f(x)=N-x^2=0的解 //x1=x2; //然后就是x2 = x1-(N-x1^2)/(-2x1)=x1/2.0+N/2.0/x1 int main() { cout<<Grial(3)<<endl; return 0; }