Re: Integer test for perfect square



In article
<13957920.1236879700767.JavaMail.jakarta@xxxxxxxxxxxxxxxxxxxxxx>,
riderofgiraffes <mathforum.org_am@xxxxxxxxxxxxxx> wrote:
Are you sure? Have you tested that? Newton's method
doubles the nubmer of digits on each iteration, but
the "long-division" method that I know so well only
gives one extra digit per phase. My speed tests show
integer variants of Newton's Method to be far faster
for large numbers.

In short, my results disagree with your assertion, and
I would be interested to see how your evidence comapares
with my tests.

Note that Newton's speed depends a lot on the initial approximation.
Consider this straightforward, if a bit ugly, implementation in
Mathematica finds Floor[Sqrt[n]]:

r = 1
For[i = 1, i < 10000, ++i,
sr = r;
r = Floor[(r + Floor[n/r])/2];
If[sr <= r && i > 1, Print[{i, sr}]; Break[]];
]

For 1000 digit numbers, this takes around 1600 iterations. Similar if
you start with r = n or r = n/2.

(I'm talking about decimal digits here, not binary digits).

Change the initialization to

r = 10^(Floor[(Length[IntegerDigits[n]] - 1)/2])

and then it only take around 12 iterations.

The IntegerDigits function return a list of the digits of the argument,
and Length returns the number of elements in the list, so
Length[IntegerDigits[n]] is simply the number of digits in n.

For numbers in the range [2^31, 2^32], it is about 20 iterations
starting with r = 1, and 7 starting with the better approximation.

If working in a language where it is easy to get the number of binary
digits in a number, then

r = 2^(Floor[(Length[IntegerDigits[n,2]] - 1)/2])

cuts off a couple more iterations.

--
--Tim Smith
.



Relevant Pages

  • Re: Convergance checking of log
    ... really takes some hundreds of iterations to converge. ... After every iteration you will have 6 times as many correct digits. ... It's probably not a great way to compute logarithms, ... converging series" I think it will be hard to beat. ...
    (sci.math)
  • Re: "Elementary" number theory problem
    ... > I am working thru some notes on Elem. ... and l the number obtained by putting the digits in increasing ... > Each number goes thru 12 iterations. ... mapped to itself by the stated operation since 7641-1467=6174] ...
    (sci.math)
  • Re: division problem
    ... lot faster than emulating hardware. ... The IBM Jikes Compiler has a 64 bit number class for 32 bit machines. ... then use the approximation given (it will be good to about 15 digits on ... 60 digits, three iterations 120 digits, etc. ...
    (comp.lang.c)
  • Re: Adding digits together
    ... multiple iterations -- which is a far more difficult problem. ... times (SumN based on N; SumSumN based on SumN; SumSumSumN based on SumSumN, ... >> arbitrary number of digits in the source number. ... > mod9 solution dodges that) ...
    (comp.databases.filemaker)
  • Re: "Elementary" number theory problem
    ... I don't know if it means anything, but the sum of the digits ... it is no great surprise that iterating the operation ... > maximum number of iterations required to converge on the fixed point ...
    (sci.math)