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

  • SEARCH
  • Home
  • 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 8087209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:42:05+00:00 2026-06-05T18:42:05+00:00

I am attempting to solve Project Euler problem 10 where the user is asked

  • 0

I am attempting to solve Project Euler problem 10 where the user is asked to calculate the sum of all the primes less than two million. I’ve written the following by studying the pseudocode on Wikipedia but the answer it generates seems to be incorrect, at least according to the website whenever I try to enter it:

int main()
{
   int limit = 2000000;
   int answer = 5;

   std::vector<bool> isPrime;
   for( int i = 0; i < limit; ++i ){
      isPrime.push_back( false );
   }

   int n = 0;
   for( int x = 1; x <= ceil( sqrt( limit ) ); ++x ){
      for( int y = 1; y <= ceil( sqrt( limit ) ); ++y ){

         n = 4*x*x + y*y;
         if( (n <= limit) && ( n%12 == 1 || n%12 == 5 ) ){
            isPrime.at(n) = ! isPrime.at(n);
         }

         n = 3*x*x + y*y;
         if( (n <= limit) && ( n%12 == 7 ) ){
            isPrime.at(n) = ! isPrime.at(n);
         }

         n = 3*x*x - y*y;
         if( (x > y) && (n <= limit) && (n%12 == 11) ){
            isPrime.at(n) = ! isPrime.at(n);
         }
      }
   }

   for( n = 6; n <= ceil( sqrt( limit ) ); n += 2 ){
      if( isPrime.at(n) ){
         for( int m = n*n; m < limit; m += n*n ){
            isPrime.at(m) = false;
         }
      }
   }

   for( int i = 5; i < limit; i += 2 ){
      if( isPrime.at(i) ){
         answer += i;
      }
   }

   std::cout << "The sum of the primes below " << limit << " is " << answer << std::endl;

   return 0;
}

The following output is generated:

The sum of all the primes below 2000000 is 1179908154

I’ve tested it with smaller limits that I’m able to verify by hand and the code is indeed functioning correctly for those numbers. I’ve found other peoples implementation which indicate that the answer should be 142913828922, but I can’t figure out where their code differs from mine.

Can anyone see what it is I’m doing wrong here?

  • 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-06-05T18:42:06+00:00Added an answer on June 5, 2026 at 6:42 pm

    You only have a signed 32-bit integer for the answer. The actual answer is much higher than can fit in 32 bits, so you have to go over to using 64 bit integers. Try using unsigned long long instead.

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

Sidebar

Related Questions

I'm attempting to solve the Project Euler Problem 8 with C++, and the problem
I am attempting to make an algorithm to solve Project Euler Problem 255 I
I'm attempting to solve the second problem on Project Euler, here is the problem:
I have sort of a tricky problem I'm attempting to solve. First of all,
Possible Duplicate: Project Euler, Problem 10 java solution not working So, I'm attempting to
I've been wrestling with Project Euler Problem #16 in C# 2.0. The crux of
I'm attempting to solve the Code Golf: Build Me an Arc problem. My solution's
I'm attempting to solve what I thought would be a very simple problem. I
I'm attempting to solve the producer-consumer problem using pthreads and semaphores, but it looks
I am attempting to solve a problem from topcoder.com and it's driving me crazy.

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.