testing for an integer square root in R

1.7k views Asked by At

How can I test if the square root of a number is an integer using R?

The following all evaluate to FALSE.

is.integer( sqrt(25) )
is.integer( sqrt(25L) )
1

There are 1 answers

7
Bastiaan Quast On

Ok, I found a work around, but I'm not sure if it's efficient or even robust to using more exotic numbers.

25 %% sqrt(25) == 0

Evaluates to TRUE.

Perhaps a better way of doing this would be:

sqrt(25) %% 1 == 0

Which also evaluates to TRUE. Adapted from this answer.