Solution by Newton Method


  • 0
    Y

    const double breaker = 0.0001;

    class Solution {
    public:
    int mySqrt(int number) {
    double Xn;
    double Xnn = 10.00;
    do
    {
    Xn = Xnn;
    Xnn= Xn-0.5*((Xn*Xn-number)/Xn);
    std::cout<<Xnn<<std::endl;
    }while(std::abs(Xn-Xnn)>breaker);

    return (int)Xnn;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.