Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Integer test for perfect square
Replies: 20   Last Post: Mar 13, 2009 12:54 PM

 Messages: [ Previous | Next ]
 jimward2@gmail.com Posts: 7 Registered: 3/12/09
Re: Integer test for perfect square
Posted: Mar 13, 2009 7:22 AM

On Mar 12, 5:00 pm, riderofgiraffes <mathforum.org...@solipsys.co.uk>
wrote:
> > Thanks! With you hints, I was able to get
> > the following Python code to work. It avoids
> > floating point, but the intermediate numbers
> > get large.

>
> > #! /usr/bin/python
>
> (snip)
>
> Why in the name of Zarquon are you doing all this?
> Here's a python routine:
>
> def TestSquareInt(n):
>     if n<0: return 0,n
>     if n<=1: return n,0
>     a = n/2
>     while True:
>         a = (a+n/a)/2
>         if a*a<=n and n<(a+1)**2:
>             return a,n-a**2
>
> All arithmetic done in integers.  This returns a pair.
> The first is the greatest integer whose square does not
> exceeding the number given, the second is the excess of
> the number of the square.  If the second number is zero
> then the given number is a square.
>
> If you object to the division, then why are you allowed
> to do a remainder operation?  It's fundamentally the
> same.

I guess I am taking the long way round! Thanks for all the hints and
the interesting discussion! I was thinking about how to implement the
long-division method on the way home yesterday, for a comparison test,
but I couldn't think of a way to grab the first few digits of an
arbitrarily sized integer.

Date Subject Author
3/12/09 jimward2@gmail.com
3/12/09 Jack Schmidt
3/12/09 Bill Dubuque
3/12/09 jimward2@gmail.com
3/12/09 Jack Schmidt
3/12/09 jimward2@gmail.com
3/12/09 riderofgiraffes
3/12/09 riderofgiraffes
3/12/09 Jack Schmidt
3/13/09 waste of time
3/13/09 jimward2@gmail.com
3/13/09 bert
3/12/09 mensanator
3/12/09 bert
3/12/09 riderofgiraffes
3/12/09 Tim Smith
3/12/09 Herman Rubin
3/13/09 bert
3/12/09 b92057@yahoo.com
3/12/09 Robert Israel
3/13/09 Michael Stemper