java biginteger not working

171
February 27, 2018, at 2:06 PM

input text 5 1 5 7 10 123456789123456789

To use the binginteger because the input value is large

Does not work on 123456789123456789. I don't know which is wrong.

For game method, two people first decide a positive integer N and have an x that is initialized with a 1.

Alice starts the game first, and then, taking turns, she does the following work on her own.

Replace x with 2x or 2x + 1.

When x is larger than N (over), the person who did the work is defeated.

My code is below.

public static Boolean result(int X, BigInteger N) {
    if(X == 0)
        return false;
    else if(X % 2 == 0) {
        BigInteger check = BigInteger.valueOf((int)((3*Math.pow(2,X)-1)/2));
        if(check.compareTo(N) == 1 || check.compareTo(N) == 0)
            return true;
        else 
            return false;
    }
    else
        return true;
}
public static int returnX(BigInteger N) {
    int X = 0;
    while(true) {
        if((BigInteger.valueOf((int)Math.pow(2,X)).compareTo(N) == -1 
                || BigInteger.valueOf((int)Math.pow(2,X)).compareTo(N) == 0)
                && N.compareTo(BigInteger.valueOf((int)Math.pow(2,X+1))) == -1)
            break;
        X++;
    }
    return X;
}
public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int T;
    T = sc.nextInt();
    BigInteger[] N = new BigInteger[T];
    for (int test_case = 1; test_case <= T; test_case++) {
        N[test_case-1] = sc.nextBigInteger();
    }
    for (int test_case = 1; test_case <= T; test_case++) {
        if(result(returnX(N[test_case-1]),N[test_case-1])) {
            System.out.printf("#%d Alice\n",test_case);
        }
        else
            System.out.printf("#%d Bob\n",test_case);
    }
}
Answer 1

You have to do all the math in BigInteger. For example,

 BigInteger.valueOf((int)((3*Math.pow(2,X)-1)/2))

should instead be

BigInteger.valueOf(3)
    .multiply(BigInteger.valueOf(2).pow(x))
    .subtract(BigInteger.ONE)
    .divide(BigInteger.valueOf(2))

...and so on through the rest of your program. If you're doing any math inside a call to BigInteger.valueOf, it's probably wrong.

Answer 2

In function return X, you wrote

if((BigInteger.valueOf((int)Math.pow(2,X)).compareTo(N) == -1 
            || BigInteger.valueOf((int)Math.pow(2,X)).compareTo(N) == 0)
            && N.compareTo(BigInteger.valueOf((int)Math.pow(2,X+1))) == -1)

it will be smaller than N forever

so try this instead:

if((BigInteger.valueOf(2).pow(X).compareTo(N) == -1
                || BigInteger.valueOf(2).pow(X).compareTo(N) == 0)
                && N.compareTo(BigInteger.valueOf(2).pow(X+1)) == -1)

it will work

READ ALSO
threadlocal webDriver &mdash; passing that webDriver to another class, how to keep threadsafe

threadlocal webDriver — passing that webDriver to another class, how to keep threadsafe

Like many before me, I'm having problems with webDriver when running tests in parallelToday, all my test classes instantiate WebDriver from within that class, and tests run in sequence within a class

143
nested if else in android studio not working

nested if else in android studio not working

I am working with chatbot in android studio the problem is when I enter start output go straight with else of choose again ? why so?

147
Kryonet server not receiving a registered class

Kryonet server not receiving a registered class

I'm making a simple chat using kryonetI've had no problems sending messages

127