How can I easily check if the square root of a number is a whole number and not a decimal without computing the square rood of said number. I could square root the number ((n^2)^1/2 or sqrt(n^2)) but I do not want to.
Asked
Active
Viewed 164 times
3
Vader
- 131
-
@RossMillikan no I meant sqrt(n^2). This will gives me n which is what I am looking for. Lets say n^2 = 36, that would mean n = 6, I want to know if n is an interger – Vader Jan 22 '14 at 16:52
-
I see what you mean, I fixed it – Vader Jan 22 '14 at 17:04
-
I would still write it as 'how to check if $n$ is a perfect square', because $n^2$ will always 'square root evenly' - it will always have $n$ as a square root (by convention, $n$ is taken as an integer). – Steven Stadnicki Jan 22 '14 at 17:14
-
As a small addendum before this (likely) gets closed, I'll note that if you want to have a good 'by hand' method for checking whether a number might be a perfect square, then the best starting point is to memorize all the possible last digits (0, 1, 4, 6, 9) and even last two digits (00, 01, 04, 09; 16; 21, 24, 25, 29; 36; 41, 44, 49; 56; 61, 64, 69; 76; 81, 84, 89; 96 - you can see the pattern here) that a square can have. – Steven Stadnicki Jan 22 '14 at 17:27
-
@StevenStadnicki I do not plan on doing this by hand, rather implement a function in python – Vader Jan 22 '14 at 17:30
-
Jebediah: if your intention is to do this for integer values (i.e., less than $2^{32}$ / $2^{64}$) then nothing you do is likely going to be faster than checking floor(sqrt(n)). – Steven Stadnicki Jan 22 '14 at 17:47
1 Answers
1
I don't know an easy way. If you know some small factors you can check that they divide $n$ an even number of times. For example if you find that $2^3|n$ but $2^4 \not |n$ you know that $n$ is not a square.
Ross Millikan
- 383,099
-
Don't forget computing Jacobi symbols, which will be much more effective when $n$ has few small divisors. – Erick Wong Jan 22 '14 at 16:56