Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » sci.math.* » sci.math

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.