title: How much time should it take to find the sum of all prime numbers less than 2 million?
body: <p>I was trying to solve this <a href="http://projecteuler.net/index.php?section=problems&amp;id=10" rel="nofollow">Project Euler Question</a>. I implemented the sieve of euler as a helper class in java. It works pretty well for the small numbers. But when I input 2 million as the limit it doesn't return the answer. I use Netbeans IDE. I waited for a lot many hours once, but it still didn't print the answer. When I stopped running the code, it gave the following result</p>
  
  <blockquote>
    <p>Java Result: 2147483647<br>
    BUILD
    SUCCESSFUL (total time: 2,097 minutes
    43 seconds)</p>
  </blockquote>
  
  <p>This answer is incorrect. Even after waiting for so much time, this isn't correct. While the same code returns correct answers for smaller limits.</p>
  
  <p>Sieve of euler has a very simple algo given at the botton of this <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">page</a>.</p>
  
  <p>My implementation is this:</p>
  
  <pre><code>package support;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   *
   * @author admin
   */
  public class SieveOfEuler {
      int upperLimit;
      List&lt;Integer&gt; primeNumbers;
  
      public SieveOfEuler(int upperLimit){
          this.upperLimit = upperLimit;
          primeNumbers = new ArrayList&lt;Integer&gt;();
          for(int i = 2 ; i &lt;= upperLimit ; i++)
              primeNumbers.add(i);
          generatePrimes();
      }
  
      private void generatePrimes(){
          int currentPrimeIndex = 0;
          int currentPrime = 2;
          while(currentPrime &lt;= Math.sqrt(upperLimit)){
              ArrayList&lt;Integer&gt; toBeRemoved = new ArrayList&lt;Integer&gt;();
              for(int i = currentPrimeIndex ; i &lt; primeNumbers.size() ; i++){
                  int multiplier = primeNumbers.get(i);
                  toBeRemoved.add(currentPrime * multiplier);
              }
  
              for(Integer i : toBeRemoved){
                  primeNumbers.remove(i);
              }
  
              currentPrimeIndex++;
              currentPrime = primeNumbers.get(currentPrimeIndex);
          }
      }
  
      public List getPrimes(){
          return primeNumbers;
      }
  
      public void displayPrimes(){
          for(double i : primeNumbers)
              System.out.println(i);
      }
  }
  </code></pre>
  
  <p>I am perplexed! My questions is</p>
  
  <p>1) Why is it taking so much time? Is there something wrong in what I am doing?</p>
  
  <p>Please suggest ways for improving my coding style, if you find something wrong.</p>
  
  <p>Question Updated:</p>
  
  <p>Here is the code, where I calculate the sum of the the calculated prime numbers:</p>
  
  <pre><code>package projecteuler;
  
  import java.io.IOException;
  import java.math.BigInteger;
  import java.util.ArrayList;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import support.SieveOfEuler;
  
  /**
   *
   * @author admin
   */
  public class Problem10 {
      private int upperLimit;
      private BigInteger sumOfPrimes;
      public void getInput() {
          try {
              System.out.println("Enter the upper limit");
              upperLimit = Integer.parseInt(br.readLine());
          } catch (IOException ex) {
              Logger.getLogger(Problem10.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      public void execute() {
          BigInteger sum = new BigInteger("0");
          SieveOfEuler soe = new SieveOfEuler(upperLimit);
          ArrayList&lt;Integer&gt; primeNumbers = (ArrayList&lt;Integer&gt;)soe.getPrimes();
          for(int i : primeNumbers){
              sum = sum.add(new BigInteger(Integer.toString(i))) ;
          }
          System.out.println(sum);
      }
  
      public void printOutput() {
         //System.out.println(sumOfPrimes);
      } 
  }
  </code></pre>