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 7835243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:34:07+00:00 2026-06-02T13:34:07+00:00

So I am attempting Problem 50 of project euler. (So close to level 2

  • 0

So I am attempting Problem 50 of project euler. (So close to level 2 :D) It goes like this:

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13  

This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?

Here is my code:

#include <iostream>
#include <vector>
using namespace std;

int main(){
    vector<int> primes(1000000,true);
    primes[0]=false;
    primes[1]=false;
    for (int n=4;n<1000000;n+=2)
        primes[n]=false;
    for (int n=3;n<1000000;n+=2){
        if (primes[n]==true){
            for (int b=n*2;b<100000;b+=n)
                primes[b]=false;
        }
    }
    int basicmax,basiccount=1,currentcount,biggermax,biggercount=1,sum=0,basicstart,basicend,biggerstart,biggerend;
    int limit=1000000;
        for (int start=2;start<limit;start++){
            //cout<<start;
            sum=0;
            currentcount=0;
            for (int basic=start;start<limit&&sum+basic<limit;basic++){
                if (primes[basic]==true){
                    //cout<<basic<<endl;
                    sum+=basic;currentcount++;}
                    if (primes[sum]&&currentcount>basiccount&&sum<limit)
                        {basicmax=sum;basiccount=currentcount;basicstart=start;basicend=basic;}

            }
            if (basiccount>biggercount)
                {biggercount=basiccount;biggermax=basicmax;biggerend=basicend;biggerstart=basicstart;}
        }
    cout<<biggercount<<endl<<biggermax<<endl;
    return 0;
}

Basically it just creates a vector of all primes up to 1000000 and then loops through them finding the right answer. The answer is 997651 and the count is supposed to be 543 but my program outputs 997661 and 546 respectively. What might be wrong?

  • 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-02T13:34:08+00:00Added an answer on June 2, 2026 at 1:34 pm

    It looks like you’re building your primes vector wrong

            for (int b=n*2;b<100000;b+=n)
                primes[b]=false;
    

    I think that should be 1,000,000 not 100,000. It might be better to refactor that number out as a constant to make sure it’s consistent throughout.

    The rest of it looks basically fine, although without testing it ourselves I’m not sure what else we can add. There’s plenty of room for efficiency improvements: you do do a lot of repeated scanning of ranges e.g. there’s no point starting to sum when prime[start] is false, you could build a second vector of just the primes for the summing etc. (Does project Euler have runtime and memory limit restrictions? I can’t remember)

    • 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
Possible Duplicate: Project Euler, Problem 10 java solution not working So, I'm attempting to
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:
This is actually a solution to Project Euler Problem 14 in F#. However, I'm
I've been wrestling with Project Euler Problem #16 in C# 2.0. The crux of
I'm getting a ton of errors like this when attempting to build an empty
I'm attempting to get my feet wet with python on Project Euler, but I'm
I'm attempting to learn Ruby on Rails by creating a project and I can't
I'm hit a problem when attempting to deploy a MVC app as a sub-application

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.