
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,na**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 longdivision 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.

