Search code examples
rintegermpfrperfect-square

How to check if a number is a perfect square by using mpfr package in R?


I have a determinant which I know is a square of an integer, but because its value is bigger than .Machine$integer.max I used the mpfr package.

But I still have problems.

Here is the algorithm:

> a<- mpfr(sqrt(det(M)), precBits=512);a
1 'mpfr' number of precision  512   bits 
[1] 430080000000001.1875

Could you please help me?


Solution

  • Is performance an issue? If not, then the following should work.

    > x<-mpfr(31415926535897932384626433832795, 500)
    > is.whole(sqrt(x))
    [1] FALSE
    
    
    > y<-mpfr(31415926535897932384626433832794, 500)^2
    > y
    1 'mpfr' number of precision  500   bits
    [1] 986960440108935918772069008410384076085841574993068761741787136
    > is.whole(sqrt(y))
    [1] TRUE