Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:15:28+00:00 2026-05-26T05:15:28+00:00

Possible Duplicate: How much time should it take to find the sum of all

  • 0

Possible Duplicate:
How much time should it take to find the sum of all prime numbers less than 2 million?

I am trying to implement a simple sieve of erathosthenes to solve this question on project euler :

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Link

My code keeps returning the wrong answer though – I keep getting 142889228620 which project euler isn’t accepting.

Can anyone give me any hints as to why? Here is the code:

import java.math.BigInteger;

public class Prime {
    /*
     * Input: an integer n > 1
     * 
     * Let A be an array of bool values, indexed by integers 2 to n, initially
     * all set to true.
     * 
     * for i = 2, 3, 4, ..., while i^2 ≤ n: if A[i] is true: for j = i^2, i^2 +
     * i, i^2 + 2i, ..., while j ≤ n: A[j] = false
     * 
     * Now all i such that A[i] is true are prime.
     */

        import java.math.BigInteger;

public class Prime {
    /*
     * Input: an integer n > 1
     * 
     * Let A be an array of bool values, indexed by integers 2 to n, initially
     * all set to true.
     * 
     * for i = 2, 3, 4, ..., while i^2 ≤ n: if A[i] is true: for j = i^2, i^2 +
     * i, i^2 + 2i, ..., while j ≤ n: A[j] = false
     * 
     * Now all i such that A[i] is true are prime.
     */

    public static void main(String[] args) {
        boolean[] array = new boolean[2000000];
        BigInteger counter = new BigInteger("0");
        for (int value = 0; value < array.length; value++) {
            array[value] = true;
        }
        for (int i = 2; i < array.length; i++) {
            if (array[i]) {
                int j = i * i;
                while (j > 0 && j < array.length) {
                    array[j] = false;
                    j += i;
                }
            }
        }
        for (int i = 2; i < array.length; i++) {
            if (array[i]) {
                counter = counter.add(BigInteger.valueOf(i));
            }
        }
        for (int value = 2; value < array.length; value++) {
            if(array[value]){
                System.out.println(value + ", ");
            }
        }
        System.out.println("\n" + counter);

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:15:29+00:00Added an answer on May 26, 2026 at 5:15 am

    This will overflow for large values of i:

    int j = i * i;
    

    One way of solving this is using long instead, as that will not overflow with numbers of this size. You can then also remove the test j > 0, as j can only go negative if you get overflows. I’m guessing you added that test because it crashed without it, but missed that the overflow can go so far that you can also get bad positive values, which causes the wrong numbers to be crossed out in your sieve, giving you incorrect results.

    One example of how this causes trouble is the prime i = 92683. Using int arithmetic, i * i overflows to 203897, which is also a prime, but will be incorrectly crossed out.

    Here is a simple fix using long arithmetic:

    for (long i = 2; i < array.length; i++) {
        if (array[(int)i]) {
            long j = i * i;
            while (j < array.length) {
                array[(int)j] = false;
                j += i;
            }
        }
    }
    

    I’ve verified that this gives the correct answer.

    We can improve further on this by noting that once i*i >= array.length, we can break out of the outer loop, as the inner loop will never run. We can do this by changing the condition of the outer loop to:

    for(int i = 2; i * i < array.length; i++)
    

    Here, we can use int again, as the loop will end before the numbers get so large that they will overflow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: python: which file is newer & by how much time In python
Possible Duplicate: c++ using too much cpu my game uses over 50% of cpu.
Possible Duplicate: Merging: hg/git vs. svn Hi, I'm a long time SVN user and
Possible Duplicate: How much does it cost to develop an iphone application? Hope somebody
Possible Duplicate: Why do people use __(double underscore) so much in C++ I was
Possible Duplicate: Why use Finally in Try … Catch Why should we not use
Possible Duplicate: get difference in time in HH:MM format php my time format is
Possible Duplicate: adding one day to a date I'm trying to add a day
Possible Duplicate: Measure execution time for a Java method I have an application by
I'm starting a new project, trying to do things right this time(so more than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.