-3

My code's assignment is to show if a number does divide with numbers 1-12 or not. If it does then it should print ("1"), if not ("0").

Example:

Input == 6;
Output == 111001000000;

The problem is that "%" does not work with BigInteger's. I have searched for a solution and found that BigInteger mod(BigInteger other) should do the the job, but i can not really catch it how could i use it, because every time and every way I try to use it. It throws an error.

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger bi = sc.nextBigInteger();

        for (int i = 1; i <= 12; i++) {
            if (bi % i == 0) {
                System.out.print("1");
            }
            if (bi % i != 0) {
                System.out.print("0");
            }
        }
    }
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
waynekenoff
  • 47
  • 1
  • 6
  • 2
  • You didn't show the non-working code with `mod`, but I'm guessing it has something to do with trying to use the `int` instead of a `BigInteger` as a parameter to `mod` - [Converting from Integer, to BigInteger](https://stackoverflow.com/q/3878192) – Bernhard Barker Feb 11 '18 at 11:06
  • functional oneliner: Stream biStream = LongStream.range(1, 12).mapToObj((i) -> BigInteger.valueOf(i)).map((BigInteger d) -> (bi.mod(d).equals(BigInteger.ZERO) ? "1\n" : "0\n")); – user1661303 Feb 11 '18 at 11:46

1 Answers1

1

When you use Integer, jdk will auto-box and auto-unbox for you. So you can just modify them with operator %, just like using primitive values. While jdk will not auto-box or auto-unbox BigInteger, you have to call mod method explicitly.

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger bi = sc.nextBigInteger();

        for (int i = 1; i <= 12; i++) {
            if (bi.mod(BigInteger.valueOf(i)).equals(BigInteger.ZERO)) {
                System.out.print("1");
            } else {
                System.out.print("0");
            }
        }
    }
}
xingbin
  • 27,410
  • 9
  • 53
  • 103