Проверьте, не является ли BigInteger идеальным квадратом - PullRequest
8 голосов
/ 21 апреля 2010

У меня есть значение BigInteger, скажем, оно 282 и находится внутри переменной x. Теперь я хочу написать цикл while, в котором говорится:

while b2 isn't a perfect square:
    a ← a + 1
    b2 ← a*a - N
endwhile

Как бы я это сделал, используя BigInteger?

РЕДАКТИРОВАТЬ: Цель для этого, чтобы я мог написать этот метод . Как говорится в статье, нужно проверить, не является ли b2 квадратным.

Ответы [ 4 ]

11 голосов
/ 21 апреля 2010

Вычислите целочисленный квадратный корень, затем проверьте, чтобы его квадрат был вашим числом. Вот мой метод вычисления квадратного корня с использованием метода Герона :

private static final BigInteger TWO = BigInteger.valueOf(2);


/**
 * Computes the integer square root of a number.
 *
 * @param n  The number.
 *
 * @return  The integer square root, i.e. the largest number whose square
 *     doesn't exceed n.
 */
public static BigInteger sqrt(BigInteger n)
{
    if (n.signum() >= 0)
    {
        final int bitLength = n.bitLength();
        BigInteger root = BigInteger.ONE.shiftLeft(bitLength / 2);

        while (!isSqrt(n, root))
        {
            root = root.add(n.divide(root)).divide(TWO);
        }
        return root;
    }
    else
    {
        throw new ArithmeticException("square root of negative number");
    }
}


private static boolean isSqrt(BigInteger n, BigInteger root)
{
    final BigInteger lowerBound = root.pow(2);
    final BigInteger upperBound = root.add(BigInteger.ONE).pow(2);
    return lowerBound.compareTo(n) <= 0
        && n.compareTo(upperBound) < 0;
}
2 голосов
/ 21 апреля 2010

Я нашел метод sqrt, используемый здесь , и упростил квадратный тест.

private static final BigInteger b100 = new BigInteger("100");
private static final boolean[] isSquareResidue;
static{
    isSquareResidue = new boolean[100];
    for(int i =0;i<100;i++){
        isSquareResidue[(i*i)%100]=true;
    }
}

public static boolean isSquare(final BigInteger r) {
    final int y = (int) r.mod(b100).longValue();
    boolean check = false;
    if (isSquareResidue[y]) {
        final BigInteger temp = sqrt(r);
        if (r.compareTo(temp.pow(2)) == 0) {
            check = true;
        }
    }
    return check;
}

public static BigInteger sqrt(final BigInteger val) {
    final BigInteger two = BigInteger.valueOf(2);
    BigInteger a = BigInteger.ONE.shiftLeft(val.bitLength() / 2);
    BigInteger b;
    do {
        b = val.divide(a);
        a = (a.add(b)).divide(two);
    } while (a.subtract(b).abs().compareTo(two) >= 0);
    return a;
}
1 голос
/ 15 сентября 2017

Я использую это:

SQRPerfect - это число, которое я хочу проверить: Это также имеет хороший квадратный корень, так что вы можете использовать это, если вам это нужно отдельно. Вы можете немного ускориться, если вы введете квадратный корень в тест Perfect Square, но мне нравится иметь обе функции.

if(PerfectSQR(SQRPerfect)){
    Do Something
}


public static Boolean PerfectSQR(BigInteger A) {
    Boolean p=false;
    BigInteger B=SQRT(A);
    BigInteger C=B.multiply(B);
    if (C.equals(A)){p=true;}
    return p;
}

public static BigInteger SQRT(BigInteger A) {
    BigInteger a=BigInteger.ONE,b=A.shiftRight(5).add(BigInteger.valueOf(8));
    while ((b.compareTo(a))>=0){
        BigInteger mid = a.add(b).shiftRight(1);
        if (mid.multiply(mid).compareTo(A)>0){b=mid.subtract(BigInteger.ONE);}
        else{a=mid.add(BigInteger.ONE);}
    }
  return a.subtract(BigInteger.ONE);
}
0 голосов
/ 21 апреля 2010

НЕ используйте это ...

 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
 }

Как прокомментировал Кристиан Семрау ниже - это не работает.Прошу прощения за неправильный ответ.

...