Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's another way:</p> <pre><code>boolean isPrime(long n) { if(n &lt; 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i &lt;= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } </code></pre> <p>and <a href="http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html#isProbablePrime%28int%29" rel="noreferrer"><code>BigInteger's isProbablePrime(...)</code></a> is valid for all 32 bit <code>int</code>'s.</p> <p><strong>EDIT</strong></p> <p>Note that <code>isProbablePrime(certainty)</code> does not always produce the correct answer. When the certainty is on the low side, it produces false positives, as @dimo414 mentioned in the comments.</p> <p>Unfortunately, I could not find the source that claimed <code>isProbablePrime(certainty)</code> is valid for all (32-bit) <code>int</code>'s (given enough certainty!).</p> <p>So I performed a couple of tests. I created a <code>BitSet</code> of size <code>Integer.MAX_VALUE/2</code> representing all uneven numbers and used a prime sieve to find all primes in the range <code>1..Integer.MAX_VALUE</code>. I then looped from <code>i=1..Integer.MAX_VALUE</code> to test that every <code>new BigInteger(String.valueOf(i)).isProbablePrime(certainty) == isPrime(i)</code>.</p> <p>For certainty 5 and 10, <code>isProbablePrime(...)</code> produced false positives along the line. But with <code>isProbablePrime(15)</code>, no test failed.</p> <p>Here's my test rig:</p> <pre><code>import java.math.BigInteger; import java.util.BitSet; public class Main { static BitSet primes; static boolean isPrime(int p) { return p &gt; 0 &amp;&amp; (p == 2 || (p%2 != 0 &amp;&amp; primes.get(p/2))); } static void generatePrimesUpTo(int n) { primes = new BitSet(n/2); for(int i = 0; i &lt; primes.size(); i++) { primes.set(i, true); } primes.set(0, false); int stop = (int)Math.sqrt(n) + 1; int percentageDone = 0, previousPercentageDone = 0; System.out.println("generating primes..."); long start = System.currentTimeMillis(); for(int i = 0; i &lt;= stop; i++) { previousPercentageDone = percentageDone; percentageDone = (int)((i + 1.0) / (stop / 100.0)); if(percentageDone &lt;= 100 &amp;&amp; percentageDone != previousPercentageDone) { System.out.println(percentageDone + "%"); } if(primes.get(i)) { int number = (i * 2) + 1; for(int p = number * 2; p &lt; n; p += number) { if(p &lt; 0) break; // overflow if(p%2 == 0) continue; primes.set(p/2, false); } } } long elapsed = System.currentTimeMillis() - start; System.out.println("finished generating primes ~" + (elapsed/1000) + " seconds"); } private static void test(final int certainty, final int n) { int percentageDone = 0, previousPercentageDone = 0; long start = System.currentTimeMillis(); System.out.println("testing isProbablePrime(" + certainty + ") from 1 to " + n); for(int i = 1; i &lt; n; i++) { previousPercentageDone = percentageDone; percentageDone = (int)((i + 1.0) / (n / 100.0)); if(percentageDone &lt;= 100 &amp;&amp; percentageDone != previousPercentageDone) { System.out.println(percentageDone + "%"); } BigInteger bigInt = new BigInteger(String.valueOf(i)); boolean bigIntSays = bigInt.isProbablePrime(certainty); if(isPrime(i) != bigIntSays) { System.out.println("ERROR: isProbablePrime(" + certainty + ") returns " + bigIntSays + " for i=" + i + " while it " + (isPrime(i) ? "is" : "isn't" ) + " a prime"); return; } } long elapsed = System.currentTimeMillis() - start; System.out.println("finished testing in ~" + ((elapsed/1000)/60) + " minutes, no false positive or false negative found for isProbablePrime(" + certainty + ")"); } public static void main(String[] args) { int certainty = Integer.parseInt(args[0]); int n = Integer.MAX_VALUE; generatePrimesUpTo(n); test(certainty, n); } } </code></pre> <p>which I ran by doing:</p> <pre><code>java -Xmx1024m -cp . Main 15 </code></pre> <p>The generating of the primes took ~30 sec on my machine. And the actual test of all <code>i</code> in <code>1..Integer.MAX_VALUE</code> took around 2 hours and 15 minutes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload