Friday, September 9, 2011

one foot in front of the other

The next Project Euler problem I'm working on requires testing if a number is a square. There are some very complicated solutions for this but the one I'm going to try isn't very complicated at all which makes me wonder if I'm doing something wrong.

Basically if I have a number X I'm taking the square root of that number, multiplying it by itself and testing to see if I get the original X back out again. Usually that would result in all numbers passing (says my calculator), but for reasons I don't completely understand yet my implementation works. I thought that I would have to take the result of the square root (which would be returned as a float), cast it to an integer, and then square and compare. This isn't the case for some reason. I need to read up on what sqrt() is actually doing.



EDIT: On a hunch I built this program on my Mac and it totally doesn't work right. I got lucky on Windows.

EDIT 2: For the life of me I can't find the implementation of that function anywhere. That's ok. I'll just be really explicit about casting and I should get the results I want.

No comments:

Post a Comment