Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few issues here. First, lets talk about the algorithm. Your <code>isPrime</code> method is actually the very thing that the sieve is designed to avoid. When you get to a number in the sieve, <em>you already know it's prime</em>, you don't need to test it. If it weren't prime, it would already have been eliminated as a factor of a lower number. </p> <p>So, point 1:</p> <ul> <li>You can eliminate the <code>isPrime</code> method altogether. It should never return false.</li> </ul> <p>Then, there are implementation issues. <code>primes.contains</code> and <code>primes.remove</code> are problems. They run in linear time on an <code>ArrayList</code>, because they require checking each element or rewriting a large portion of the backing array.</p> <p>Point 2:</p> <ul> <li>Either mark values in place (use <code>boolean[]</code>, or use some other more appropriate data structure.)</li> </ul> <p>I typically use something like <code>boolean primes = new boolean[upper+1]</code>, and define <code>n</code> to be included if <code>!(primes[n])</code>. (I just ignore elements 0 and 1 so I don't have to subtract indices.) To "remove" an element, I set it to true. You could also use something like <code>TreeSet&lt;Integer&gt;</code>, I suppose. Using <code>boolean[]</code>, the method is near-instantaneous. </p> <p>Point 3:</p> <ul> <li><code>sum</code> needs to be a long. The answer (roughly 1.429e11) is larger than the maximum value of an integer (2^31-1)</li> </ul> <p>I can post working code if you like, but here's a test output, without spoilers:</p> <pre><code>public static void main(String[] args) { long value; long start; long finish; start = System.nanoTime(); value = arrayMethod(2000000); finish = System.nanoTime(); System.out.printf("Value: %.3e, time: %4d ms\n", (double)value, (finish-start)/1000000); start = System.nanoTime(); value = treeMethod(2000000); finish = System.nanoTime(); System.out.printf("Value: %.3e, time: %4d ms\n", (double)value, (finish-start)/1000000); } </code></pre> <p>output:</p> <pre><code>Using boolean[] Value: 1.429e+11, time: 17 ms Using TreeSet&lt;Integer&gt; Value: 1.429e+11, time: 4869 ms </code></pre> <p><strong>Edit:</strong> Since spoilers are posted, here's my code:</p> <pre><code>public static long arrayMethod(int upper) { boolean[] primes = new boolean[upper+1]; long sum = 0; for (int i = 2; i &lt;=upper; i++) { if (!primes[i]) { sum += i; for (int k = 2*i; k &lt;= upper; k+=i) { primes[k] = true; } } } return sum; } public static long treeMethod(int upper) { TreeSet&lt;Integer&gt; primes = new TreeSet&lt;Integer&gt;(); for (int i = 2; i &lt;= upper; i++) { primes.add(i); } long sum = 0; for (Integer i = 2; i != null; i=primes.higher(i)) { sum += i; for (int k = 2*i; k &lt;= upper; k+=i) { primes.remove(k); } } return sum; } </code></pre>
    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. 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