Re: What do you think?

Use the built in square root function, then take the floor and the ceiling of that number. Add one and subtract one to each one. Now square each one and match it to the number you are testing.

That should take care of round off error either way.

How is that different from the code code I have proposed?!

Here lies the reader who will never open this book. He is forever dead.Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and PunishmentThe knowledge of some things as a function of age is a delta function.

Re: What do you think?

Of those 4 numbers, 2 will never be the square root of the given number, with rounding errors or without...

Here lies the reader who will never open this book. He is forever dead.Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and PunishmentThe knowledge of some things as a function of age is a delta function.

Re: What do you think?

We want our number to be a square of an integer, not a real number... Do you have an example in which my code will not get the correct answer and yours will?

Last edited by anonimnystefy (2013-01-21 22:27:17)

Here lies the reader who will never open this book. He is forever dead.Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and PunishmentThe knowledge of some things as a function of age is a delta function.

Re: What do you think?

Hi bobbym,

Good idea, but I liked your integer only suggestion better since the code dealing with integers would be faster than floating points. Also, that algorithm on wikipedia did not even have integer divisions, only shift operations!

"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense" - Buddha?

Re: What do you think?

Hi;

I was not suggesting rewriting a floating point routine. You would be using the built in square root routine as an estimate. It is probably implemented in the math coprocessor and would not use much time at all.

The integer square root does have the advantage of being verified for any other idea you would have to prove that it never missed. That means checking 4 billion square roots once.

In mathematics, you don't understand things. You just get used to them.If it ain't broke, fix it until it is.No great discovery was ever made without a bold guess.