-2

I'm currently in the process of doing an assignment for one of my class using the BigInteger class. The problem I'm running into is with the .pow method. its giving me this error BigNumberExample1.java:25: error: incompatible types: BigInteger cannot be converted to int BigInteger input4 = (a.pow(b).mod(n));

import java.io.*;
import java.math.*;
class BigNumberExample1{

   public static void main (String args[])
   {
      try 
      {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter number a");
      String A = in.readLine();
      System.out.println("Enter number b");
      String B = in.readLine();

      BigInteger a = new BigInteger(A);
      BigInteger b = new BigInteger(B);
      BigInteger input1 = (a.xor(b));
      BigInteger input2 = (a.or(b));
      BigInteger input3 = (a.or(b).and(a));    
      System.out.println("Enter number n");
      String N = in.readLine();
      BigInteger n = new BigInteger(N);
      BigInteger input4 = (a.pow(b).mod(n));
      BigInteger input5 = (a.shiftRight(5));
      BigInteger input6 = (b.shiftLeft(7));
      System.out.println(input1);
      System.out.println(input2);
      System.out.println(input3);
      System.out.println(input4);
      System.out.println(input5);
      System.out.println(input6);
      }
      catch (Exception e) 
      {
         e.printStackTrace();
      }
   }
Reed Sager
  • 25
  • 3
  • 2
    there is another method called modPow, have you considered using it instead of that code? also note that pow() gets int as argument not BigInteger – Pooya Jan 27 '16 at 01:58
  • The error message is clear enough. What do you not understand about it? – Raedwald Jan 27 '16 at 02:36

3 Answers3

1

If you see the document, it takes an int value in pow():

pow(int exponent)

so do:

a.pow(b.intValue()).mod(...)
Even Cheng
  • 1,986
  • 19
  • 14
1

The problem is that BigInteger.pow() takes an int and not a BigInteger. Since b is a BigInteger you could do something like:

BigInteger input4 = a.pow(b.intValue()).mod(n);

but that will break if b is bigger than an int. Your best bet is to use BigInteger.modPow() instead since that takes 2 BigIntegers. Then you end up with:

BigInteger input4 = a.modPow(b, n);
Windle
  • 1,385
  • 2
  • 14
  • 33
0

looks to me like a.pow(b) is expecting 'b' to be an integer, not a big int

Try BigInteger input4 = (a.pow(b.intValue()).mod(n));