Check if BigInteger is not a perfect square

后端 未结 6 1707
悲&欢浪女
悲&欢浪女 2020-12-29 14:12

I have a BigInteger value, let\'s say it is 282 and is inside the variable x. I now want to write a while loop that states:

while b2 isn\'t a perfect square:         


        
6条回答
  •  醉酒成梦
    2020-12-29 14:45

    DON'T use this...

     BigInteger n = ...;
     double n_as_double = n.doubleValue();
     double n_sqrt = Math.sqrt(n_as_double);
     BigInteger n_sqrt_as_int = new BigDecimal(n_sqrt).toBigInteger();
     if (n_sqrt_as_int.pow(2).equals(n)) {
      // number is perfect square
     }
    

    As Christian Semrau commented below - this doesn't work. I am sorry for posting incorrect answer.

提交回复
热议问题