Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Modulus with BigIntegers

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Modulus with BigIntegers

    Hey, so I'm trying to make a method that checks if a BigInteger is prime, but I'm running into an issue in doing the modulus. Below is the whole method.

    public static boolean isPrimeItself(BigInteger input)
    {
    boolean answer = true;
    BigInteger zero = new BigInteger(""+0+"");
    for(int i=2;i<100/*I wasn't sure what to make this because I want it to be input but input is a BigInt...*/;i++) {
    BigInteger temp = new BigInteger(""+i+"");
    System.out.println(temp);
    if (temp.remainder(input)==zero)
    {
    answer = false;
    System.out.println("Success!");
    }
    }
    return answer;
    }

    I included the print lines to see where it was going wrong, and the if test [if (temp.remainder(input)==zero)] is never being activated. Did I make a mistake in using the remainder method? Upon researching I know there seems to be a premade method that does this, but I'm really just trying to practice using BigIntegers. Thanks for any comments / help you can provide!


  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Modulus with BigIntegers

    Got it! Silly mistake. I should have used compareTo instead of ==. My current issue is that I need to use BigIntegers in the conditions of the for loop, is that not allowed? Because my method seems to get stuck in a loop when I try it.

  3. #3
    Moderators helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Modulus with BigIntegers

    You'll have to convert everything to a BigInteger, then use the compareTo method.

    for(BigInteger i = new BigInteger("2"); i.compareTo(input) < 0; i = i.add(BigInteger.ONE))

    However, I would strongly advise against writing a naive primality test for BigInteger numbers. There are much faster statistical methods for checking if a number is prime.

    One of these is built into the BigInteger class: isProbablyPrime.

    There is also another method I implemented here. This uses a "better" method for checking if a number is prime for twice the statistical accuracy. This is because this test always reports composite numbers as composite and prime numbers as probably prime, but the built-in method reports both probably prime and probably composite.

Tags for this Thread