What is the computational complexity of Newton Raphson method to find square root.












0












$begingroup$


I am not a math student, so I don’t fully understand the complexity as mentioned on Wiki for Newton Raphson method for finding square root. But I wrote a computer program for Newton-Raphson’s method and tried to run on increasing values.



/*Code Snippet */
x = n = 100;
y = 1
i = 0;
while x > y:
x = (x+y)/2
y = n/x
i = i + 1
print "Iterations for convergence: ", i


Values: ( All log values are base 2)*



N=10000 , Iterations : 9 , log(N) = 13.28



N=100000000 , Iterations : 16 , log(N) = 26.57



N=10000000000000000 , Iterations : 30 , log(N) = 53.15



…..and so on…..Assuming the 2 division takes constant value , is the complexity of the method less than log(N)? Atleast that is what I could see from my values. Can someone please try to answer in layman terms if NR method is less than logN or it is greater than equal to logN. You can assume for perfect squares for now.










share|cite|improve this question









$endgroup$








  • 1




    $begingroup$
    I do not know the exact complexity, but considering that each iteration roughly doubles the number of correct digits, the complexity should be near $O(log(N))$
    $endgroup$
    – Peter
    Jul 20 '16 at 18:25










  • $begingroup$
    @Peter I disagree. The number of correct digits roughly doubles once you are within a neighorhood of the solution (e.g. you have one correct digit). When you are far away the situation is more specific to this particular method rather than the general theory of Newton's method.
    $endgroup$
    – Ian
    Jul 21 '16 at 0:20


















0












$begingroup$


I am not a math student, so I don’t fully understand the complexity as mentioned on Wiki for Newton Raphson method for finding square root. But I wrote a computer program for Newton-Raphson’s method and tried to run on increasing values.



/*Code Snippet */
x = n = 100;
y = 1
i = 0;
while x > y:
x = (x+y)/2
y = n/x
i = i + 1
print "Iterations for convergence: ", i


Values: ( All log values are base 2)*



N=10000 , Iterations : 9 , log(N) = 13.28



N=100000000 , Iterations : 16 , log(N) = 26.57



N=10000000000000000 , Iterations : 30 , log(N) = 53.15



…..and so on…..Assuming the 2 division takes constant value , is the complexity of the method less than log(N)? Atleast that is what I could see from my values. Can someone please try to answer in layman terms if NR method is less than logN or it is greater than equal to logN. You can assume for perfect squares for now.










share|cite|improve this question









$endgroup$








  • 1




    $begingroup$
    I do not know the exact complexity, but considering that each iteration roughly doubles the number of correct digits, the complexity should be near $O(log(N))$
    $endgroup$
    – Peter
    Jul 20 '16 at 18:25










  • $begingroup$
    @Peter I disagree. The number of correct digits roughly doubles once you are within a neighorhood of the solution (e.g. you have one correct digit). When you are far away the situation is more specific to this particular method rather than the general theory of Newton's method.
    $endgroup$
    – Ian
    Jul 21 '16 at 0:20
















0












0








0





$begingroup$


I am not a math student, so I don’t fully understand the complexity as mentioned on Wiki for Newton Raphson method for finding square root. But I wrote a computer program for Newton-Raphson’s method and tried to run on increasing values.



/*Code Snippet */
x = n = 100;
y = 1
i = 0;
while x > y:
x = (x+y)/2
y = n/x
i = i + 1
print "Iterations for convergence: ", i


Values: ( All log values are base 2)*



N=10000 , Iterations : 9 , log(N) = 13.28



N=100000000 , Iterations : 16 , log(N) = 26.57



N=10000000000000000 , Iterations : 30 , log(N) = 53.15



…..and so on…..Assuming the 2 division takes constant value , is the complexity of the method less than log(N)? Atleast that is what I could see from my values. Can someone please try to answer in layman terms if NR method is less than logN or it is greater than equal to logN. You can assume for perfect squares for now.










share|cite|improve this question









$endgroup$




I am not a math student, so I don’t fully understand the complexity as mentioned on Wiki for Newton Raphson method for finding square root. But I wrote a computer program for Newton-Raphson’s method and tried to run on increasing values.



/*Code Snippet */
x = n = 100;
y = 1
i = 0;
while x > y:
x = (x+y)/2
y = n/x
i = i + 1
print "Iterations for convergence: ", i


Values: ( All log values are base 2)*



N=10000 , Iterations : 9 , log(N) = 13.28



N=100000000 , Iterations : 16 , log(N) = 26.57



N=10000000000000000 , Iterations : 30 , log(N) = 53.15



…..and so on…..Assuming the 2 division takes constant value , is the complexity of the method less than log(N)? Atleast that is what I could see from my values. Can someone please try to answer in layman terms if NR method is less than logN or it is greater than equal to logN. You can assume for perfect squares for now.







algorithms numerical-methods newton-raphson






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Jul 20 '16 at 18:21









Anup BuchkeAnup Buchke

10314




10314








  • 1




    $begingroup$
    I do not know the exact complexity, but considering that each iteration roughly doubles the number of correct digits, the complexity should be near $O(log(N))$
    $endgroup$
    – Peter
    Jul 20 '16 at 18:25










  • $begingroup$
    @Peter I disagree. The number of correct digits roughly doubles once you are within a neighorhood of the solution (e.g. you have one correct digit). When you are far away the situation is more specific to this particular method rather than the general theory of Newton's method.
    $endgroup$
    – Ian
    Jul 21 '16 at 0:20
















  • 1




    $begingroup$
    I do not know the exact complexity, but considering that each iteration roughly doubles the number of correct digits, the complexity should be near $O(log(N))$
    $endgroup$
    – Peter
    Jul 20 '16 at 18:25










  • $begingroup$
    @Peter I disagree. The number of correct digits roughly doubles once you are within a neighorhood of the solution (e.g. you have one correct digit). When you are far away the situation is more specific to this particular method rather than the general theory of Newton's method.
    $endgroup$
    – Ian
    Jul 21 '16 at 0:20










1




1




$begingroup$
I do not know the exact complexity, but considering that each iteration roughly doubles the number of correct digits, the complexity should be near $O(log(N))$
$endgroup$
– Peter
Jul 20 '16 at 18:25




$begingroup$
I do not know the exact complexity, but considering that each iteration roughly doubles the number of correct digits, the complexity should be near $O(log(N))$
$endgroup$
– Peter
Jul 20 '16 at 18:25












$begingroup$
@Peter I disagree. The number of correct digits roughly doubles once you are within a neighorhood of the solution (e.g. you have one correct digit). When you are far away the situation is more specific to this particular method rather than the general theory of Newton's method.
$endgroup$
– Ian
Jul 21 '16 at 0:20






$begingroup$
@Peter I disagree. The number of correct digits roughly doubles once you are within a neighorhood of the solution (e.g. you have one correct digit). When you are far away the situation is more specific to this particular method rather than the general theory of Newton's method.
$endgroup$
– Ian
Jul 21 '16 at 0:20












2 Answers
2






active

oldest

votes


















1












$begingroup$

It depends on how good your initial guess is. In your program, you take an initial guess of $x=N$. Consequently there will be an initial period where $x/2$ is way bigger than $N/x$, so that the method is essentially just cutting the number in half over and over. So it will take roughly $log_2(N/sqrt{N})=1/2 log_2(N)$ steps to get to within some fixed neighborhood of $sqrt{N}$. Then there will be a "convergence" period, which takes a number of steps that really only depends on the tolerance, not on $N$. Adding these together will give a number of steps that scales like $log_2(N)$ as $N to infty$ (for a fixed relative tolerance) but is not directly proportional to $log_2(N)$.



That said, there are better initial guesses. For instance, on a normal computer, you are given $N$ in the form $N=a 2^k$ where $a$ is between $1$ and $2$ (including $1$, not including $2$) and $k$ is an integer. In other words this representation is given to you by the machine with no additional work.



In this case you can take your initial guess to be $2^{lfloor k/2 rfloor}$ where $lfloor x rfloor$ denotes the greatest integer less than $x$. Then your initial guess is within a factor of $4$ of the desired answer, so that initial "halving" period will only take $O(1)$ time. In fact by precomputing $sqrt{2}$ you could do even better: $sqrt{a2^k}=sqrt{a}2^{k/2}=begin{cases} sqrt{2} sqrt{a}2^{lfloor k/2 rfloor} & k text{ is odd } \ sqrt{a} 2^{lfloor k/2 rfloor} & k text{ is even } end{cases}$, so now your routine can be reduced to just finding a square root of a number in $[1,2)$.






share|cite|improve this answer











$endgroup$













  • $begingroup$
    Binary search is also $log_2(N)$. How is newton's method faster than binary search? @Ian
    $endgroup$
    – qkhhly
    Jan 21 at 2:50








  • 1




    $begingroup$
    @qkhhly Starting from far enough away it isn't. It is only better once your guesses are already pretty good.
    $endgroup$
    – Ian
    Jan 21 at 2:51



















1












$begingroup$

The average theoretical complexity is $log_2(N)$. However, two notes:



$log_2(N)$ is just the highest order term. The actual number of steps to convergence might be:



constant term + coefficient*$log_2(N)$



Secondly, there can be some nuance about the exact floating point computations, etc. where convergence might occur a step sooner or later, before x and y converge to within "machine epsilon" (i.e. so close that they can't be distinguished).



Basically, don't worry. The number of steps to convergence roughly follows $log_2(N)$. If you did more iterations, it would more perfectly fit $log_2(N)$.






share|cite|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "69"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1865688%2fwhat-is-the-computational-complexity-of-newton-raphson-method-to-find-square-roo%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1












    $begingroup$

    It depends on how good your initial guess is. In your program, you take an initial guess of $x=N$. Consequently there will be an initial period where $x/2$ is way bigger than $N/x$, so that the method is essentially just cutting the number in half over and over. So it will take roughly $log_2(N/sqrt{N})=1/2 log_2(N)$ steps to get to within some fixed neighborhood of $sqrt{N}$. Then there will be a "convergence" period, which takes a number of steps that really only depends on the tolerance, not on $N$. Adding these together will give a number of steps that scales like $log_2(N)$ as $N to infty$ (for a fixed relative tolerance) but is not directly proportional to $log_2(N)$.



    That said, there are better initial guesses. For instance, on a normal computer, you are given $N$ in the form $N=a 2^k$ where $a$ is between $1$ and $2$ (including $1$, not including $2$) and $k$ is an integer. In other words this representation is given to you by the machine with no additional work.



    In this case you can take your initial guess to be $2^{lfloor k/2 rfloor}$ where $lfloor x rfloor$ denotes the greatest integer less than $x$. Then your initial guess is within a factor of $4$ of the desired answer, so that initial "halving" period will only take $O(1)$ time. In fact by precomputing $sqrt{2}$ you could do even better: $sqrt{a2^k}=sqrt{a}2^{k/2}=begin{cases} sqrt{2} sqrt{a}2^{lfloor k/2 rfloor} & k text{ is odd } \ sqrt{a} 2^{lfloor k/2 rfloor} & k text{ is even } end{cases}$, so now your routine can be reduced to just finding a square root of a number in $[1,2)$.






    share|cite|improve this answer











    $endgroup$













    • $begingroup$
      Binary search is also $log_2(N)$. How is newton's method faster than binary search? @Ian
      $endgroup$
      – qkhhly
      Jan 21 at 2:50








    • 1




      $begingroup$
      @qkhhly Starting from far enough away it isn't. It is only better once your guesses are already pretty good.
      $endgroup$
      – Ian
      Jan 21 at 2:51
















    1












    $begingroup$

    It depends on how good your initial guess is. In your program, you take an initial guess of $x=N$. Consequently there will be an initial period where $x/2$ is way bigger than $N/x$, so that the method is essentially just cutting the number in half over and over. So it will take roughly $log_2(N/sqrt{N})=1/2 log_2(N)$ steps to get to within some fixed neighborhood of $sqrt{N}$. Then there will be a "convergence" period, which takes a number of steps that really only depends on the tolerance, not on $N$. Adding these together will give a number of steps that scales like $log_2(N)$ as $N to infty$ (for a fixed relative tolerance) but is not directly proportional to $log_2(N)$.



    That said, there are better initial guesses. For instance, on a normal computer, you are given $N$ in the form $N=a 2^k$ where $a$ is between $1$ and $2$ (including $1$, not including $2$) and $k$ is an integer. In other words this representation is given to you by the machine with no additional work.



    In this case you can take your initial guess to be $2^{lfloor k/2 rfloor}$ where $lfloor x rfloor$ denotes the greatest integer less than $x$. Then your initial guess is within a factor of $4$ of the desired answer, so that initial "halving" period will only take $O(1)$ time. In fact by precomputing $sqrt{2}$ you could do even better: $sqrt{a2^k}=sqrt{a}2^{k/2}=begin{cases} sqrt{2} sqrt{a}2^{lfloor k/2 rfloor} & k text{ is odd } \ sqrt{a} 2^{lfloor k/2 rfloor} & k text{ is even } end{cases}$, so now your routine can be reduced to just finding a square root of a number in $[1,2)$.






    share|cite|improve this answer











    $endgroup$













    • $begingroup$
      Binary search is also $log_2(N)$. How is newton's method faster than binary search? @Ian
      $endgroup$
      – qkhhly
      Jan 21 at 2:50








    • 1




      $begingroup$
      @qkhhly Starting from far enough away it isn't. It is only better once your guesses are already pretty good.
      $endgroup$
      – Ian
      Jan 21 at 2:51














    1












    1








    1





    $begingroup$

    It depends on how good your initial guess is. In your program, you take an initial guess of $x=N$. Consequently there will be an initial period where $x/2$ is way bigger than $N/x$, so that the method is essentially just cutting the number in half over and over. So it will take roughly $log_2(N/sqrt{N})=1/2 log_2(N)$ steps to get to within some fixed neighborhood of $sqrt{N}$. Then there will be a "convergence" period, which takes a number of steps that really only depends on the tolerance, not on $N$. Adding these together will give a number of steps that scales like $log_2(N)$ as $N to infty$ (for a fixed relative tolerance) but is not directly proportional to $log_2(N)$.



    That said, there are better initial guesses. For instance, on a normal computer, you are given $N$ in the form $N=a 2^k$ where $a$ is between $1$ and $2$ (including $1$, not including $2$) and $k$ is an integer. In other words this representation is given to you by the machine with no additional work.



    In this case you can take your initial guess to be $2^{lfloor k/2 rfloor}$ where $lfloor x rfloor$ denotes the greatest integer less than $x$. Then your initial guess is within a factor of $4$ of the desired answer, so that initial "halving" period will only take $O(1)$ time. In fact by precomputing $sqrt{2}$ you could do even better: $sqrt{a2^k}=sqrt{a}2^{k/2}=begin{cases} sqrt{2} sqrt{a}2^{lfloor k/2 rfloor} & k text{ is odd } \ sqrt{a} 2^{lfloor k/2 rfloor} & k text{ is even } end{cases}$, so now your routine can be reduced to just finding a square root of a number in $[1,2)$.






    share|cite|improve this answer











    $endgroup$



    It depends on how good your initial guess is. In your program, you take an initial guess of $x=N$. Consequently there will be an initial period where $x/2$ is way bigger than $N/x$, so that the method is essentially just cutting the number in half over and over. So it will take roughly $log_2(N/sqrt{N})=1/2 log_2(N)$ steps to get to within some fixed neighborhood of $sqrt{N}$. Then there will be a "convergence" period, which takes a number of steps that really only depends on the tolerance, not on $N$. Adding these together will give a number of steps that scales like $log_2(N)$ as $N to infty$ (for a fixed relative tolerance) but is not directly proportional to $log_2(N)$.



    That said, there are better initial guesses. For instance, on a normal computer, you are given $N$ in the form $N=a 2^k$ where $a$ is between $1$ and $2$ (including $1$, not including $2$) and $k$ is an integer. In other words this representation is given to you by the machine with no additional work.



    In this case you can take your initial guess to be $2^{lfloor k/2 rfloor}$ where $lfloor x rfloor$ denotes the greatest integer less than $x$. Then your initial guess is within a factor of $4$ of the desired answer, so that initial "halving" period will only take $O(1)$ time. In fact by precomputing $sqrt{2}$ you could do even better: $sqrt{a2^k}=sqrt{a}2^{k/2}=begin{cases} sqrt{2} sqrt{a}2^{lfloor k/2 rfloor} & k text{ is odd } \ sqrt{a} 2^{lfloor k/2 rfloor} & k text{ is even } end{cases}$, so now your routine can be reduced to just finding a square root of a number in $[1,2)$.







    share|cite|improve this answer














    share|cite|improve this answer



    share|cite|improve this answer








    edited Jan 21 at 2:52

























    answered Jul 20 '16 at 18:44









    IanIan

    68.4k25388




    68.4k25388












    • $begingroup$
      Binary search is also $log_2(N)$. How is newton's method faster than binary search? @Ian
      $endgroup$
      – qkhhly
      Jan 21 at 2:50








    • 1




      $begingroup$
      @qkhhly Starting from far enough away it isn't. It is only better once your guesses are already pretty good.
      $endgroup$
      – Ian
      Jan 21 at 2:51


















    • $begingroup$
      Binary search is also $log_2(N)$. How is newton's method faster than binary search? @Ian
      $endgroup$
      – qkhhly
      Jan 21 at 2:50








    • 1




      $begingroup$
      @qkhhly Starting from far enough away it isn't. It is only better once your guesses are already pretty good.
      $endgroup$
      – Ian
      Jan 21 at 2:51
















    $begingroup$
    Binary search is also $log_2(N)$. How is newton's method faster than binary search? @Ian
    $endgroup$
    – qkhhly
    Jan 21 at 2:50






    $begingroup$
    Binary search is also $log_2(N)$. How is newton's method faster than binary search? @Ian
    $endgroup$
    – qkhhly
    Jan 21 at 2:50






    1




    1




    $begingroup$
    @qkhhly Starting from far enough away it isn't. It is only better once your guesses are already pretty good.
    $endgroup$
    – Ian
    Jan 21 at 2:51




    $begingroup$
    @qkhhly Starting from far enough away it isn't. It is only better once your guesses are already pretty good.
    $endgroup$
    – Ian
    Jan 21 at 2:51











    1












    $begingroup$

    The average theoretical complexity is $log_2(N)$. However, two notes:



    $log_2(N)$ is just the highest order term. The actual number of steps to convergence might be:



    constant term + coefficient*$log_2(N)$



    Secondly, there can be some nuance about the exact floating point computations, etc. where convergence might occur a step sooner or later, before x and y converge to within "machine epsilon" (i.e. so close that they can't be distinguished).



    Basically, don't worry. The number of steps to convergence roughly follows $log_2(N)$. If you did more iterations, it would more perfectly fit $log_2(N)$.






    share|cite|improve this answer









    $endgroup$


















      1












      $begingroup$

      The average theoretical complexity is $log_2(N)$. However, two notes:



      $log_2(N)$ is just the highest order term. The actual number of steps to convergence might be:



      constant term + coefficient*$log_2(N)$



      Secondly, there can be some nuance about the exact floating point computations, etc. where convergence might occur a step sooner or later, before x and y converge to within "machine epsilon" (i.e. so close that they can't be distinguished).



      Basically, don't worry. The number of steps to convergence roughly follows $log_2(N)$. If you did more iterations, it would more perfectly fit $log_2(N)$.






      share|cite|improve this answer









      $endgroup$
















        1












        1








        1





        $begingroup$

        The average theoretical complexity is $log_2(N)$. However, two notes:



        $log_2(N)$ is just the highest order term. The actual number of steps to convergence might be:



        constant term + coefficient*$log_2(N)$



        Secondly, there can be some nuance about the exact floating point computations, etc. where convergence might occur a step sooner or later, before x and y converge to within "machine epsilon" (i.e. so close that they can't be distinguished).



        Basically, don't worry. The number of steps to convergence roughly follows $log_2(N)$. If you did more iterations, it would more perfectly fit $log_2(N)$.






        share|cite|improve this answer









        $endgroup$



        The average theoretical complexity is $log_2(N)$. However, two notes:



        $log_2(N)$ is just the highest order term. The actual number of steps to convergence might be:



        constant term + coefficient*$log_2(N)$



        Secondly, there can be some nuance about the exact floating point computations, etc. where convergence might occur a step sooner or later, before x and y converge to within "machine epsilon" (i.e. so close that they can't be distinguished).



        Basically, don't worry. The number of steps to convergence roughly follows $log_2(N)$. If you did more iterations, it would more perfectly fit $log_2(N)$.







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered Jul 20 '16 at 18:29









        XSPXXSPX

        1063




        1063






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1865688%2fwhat-is-the-computational-complexity-of-newton-raphson-method-to-find-square-roo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mario Kart Wii

            Partial Derivative Guidance.

            Understanding the size os this class of aleatory events