Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4655458

BigInteger.isProbablePrime(Integer.MAX_VALUE) returns true for even numbers

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • x86
    • windows_2000



      Name: jl125535 Date: 03/20/2002


      FULL PRODUCT VERSION :
      F:\>java -version
      java version "1.3.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

      FULL OPERATING SYSTEM VERSION : win2k pro
      Verified on Solaris 8 with both JDK 1.3.1 and JDK 1.4.

      A DESCRIPTION OF THE PROBLEM :
      java.math.BigInteger.isProbablePrime(Integer.MAX_VALUE) returns true for even numbers from 4 to 1*10^6 (and probably beyond).

      Note that BigInteger.isProbablePrime(Integer.MAX_VALUE - 1) correctly returns false for those same values.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the test case

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.math.BigInteger;

      public class Test
      {
          public static void main(String[] args)
          {
              BigInteger onemill = new BigInteger(1000000 + "");
              BigInteger two = new BigInteger(2 + "");
              BigInteger test = new BigInteger(0 + "");
              while (true)
              {
                  if (test.isProbablePrime(Integer.MAX_VALUE))
                  {
                      // This line should never appear (except for 2)
                      
                      //System.out.println(test
                      // + ".isProbablePrime(MAX_VALUE) = false");
                  }
                  else
                  {
                      // This line should always appear (exepct for 2)
                      //this will only be printed if sProbablePrime is correct
                      //except test value of 2
                      System.out.println(test
                                         + ".isProbablePrime(MAX_VALUE) = false");
                   }
                  test = test.add(two);
                  if (test.equals(onemill))
                  {
                      System.out.println("If only this line appeared, the test "
                                         + "failed.");
                      break;
                  }
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      don't use Integer.MAX_VALUE for the certainty
      (Review ID: 144259)
      ======================================================================

            iris Iris Clark
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: