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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:02:34+00:00 2026-05-20T10:02:34+00:00

You may have heard of a website called Project Euler (projecteuler.net). I’m working through

  • 0

You may have heard of a website called Project Euler (projecteuler.net). I’m working through the first problems, which were quite trivial, and I’m on the problem described in the title.

This isn’t about optimising or anything – it takes about 90 thousandths of a second to complete. It’s giving me the wrong total.

Can someone help me? I have no clue why the answer I’m getting – from both the array total (atotal) and the total that was added up normally (total) – is incorrect. The answer they are both showing is 947402457, which the website it telling me is the wrong answer.

In case it’s just the wording, the question is here: http://projecteuler.net/index.php?section=problems&id=10

What’s also very strange is, as far as I can tell, when, at the end when you can type in which prime number you would like to view (it takes it out of the array), it gives you the correct answer.

#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>

typedef unsigned long int bignum;
//there are 666671 primes below two million
int main(){
    using namespace std;
    bignum top = 2000000;
    bignum total = 0;
    bignum atotal = 0;
    //hardcode 2 and 3
    total += 5;
    int inc = 2;
    bignum n = 5;
    double sq = n;
    bignum np = 1;
    bignum *pa = new bignum[top];
    pa[0] = 2;
    pa[1] = 3;
    while (n < top){
        int div = 5;
        int divinc = 2;
        int p = 1;
        //check if number is prime
        //check divisiblity from any possible prime up to the square root of n
        //first hardcode 2 and 3
        if(n%2==0||n%3==0)
            p = 0;
        else{
            while(div<=sqrt(sq)){
                if(n%div==0){
                    p = 0;
                    break;
                }else{
                    div = div + divinc;
                    if(divinc==2) divinc = 4; else divinc = 2;
                }
            }if(p!=0){ //if it's a prime - 0 is not, 1 is prime
                total = total + n;
                np++;
                pa[np] = n;
                //cout << np << " prime number: " << n << endl; //takes too long if it prints everything
            }
        }
        n += inc;
        if(inc==2) inc = 4; else inc = 2;
    }
    for (int c=0;c<=np;c++){
        atotal += pa[c];
    }
    cout << "Total " << top << ": " << total << endl;
    cout << "Array total: " << atotal << endl;
    cout << "Elapsed time: " << clock() << " " << CLOCKS_PER_SEC << "s of a second" << endl << endl;
    while(true){
            int ptoview = 0;
            cout << "Enter the number of the prime you would like to see (you can view every prime number below "<<top<<") ";
            cin >> ptoview;
            if (pa[ptoview-1]){
                if (pa[ptoview-1] < top)
                    cout << ptoview << " prime: " << pa[ptoview-1] << endl;
                else
                    cout << "Too high/low" << endl;
                cout << endl;
            }
        }
    system("PAUSE");
    return 0;
}
  • 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-20T10:02:34+00:00Added an answer on May 20, 2026 at 10:02 am

    Here’s a clue to at least one problem. Have a look at what happens when you replace:

    for (int c=0;c<=np;c++){
        atotal += pa[c];
    }
    

    with:

    for (int c=0;c<=np;c++){
        bignum oldatotal = atotal;
        atotal += pa[c];
        if (atotal < oldatotal)
            cout << "Hmmm: " << oldatotal << " " << atotal << endl;
    }
    

    I get something like:

    Hmmm: 4294819625 12858
    Hmmm: 4294864122 123849
    Hmmm: 4294717053 27802
    Hmmm: 4294697657 51420
    : : :
    Hmmm: 4293781002 792849
    Hmmm: 4294658253 1676602
    Hmmm: 4293686116 710941
    Hmmm: 4294706293 1737578
    Total 2000000: 947402457
    Array total: 947402457
    

    I won’t go into the detail since this is a puzzle and I’m assuming you want to keep it at least a little challenging 🙂


    And yes, you’re right (based on your comment below) so I’ll make the answer a little less obtuse so it’s more useful for others.

    The unsigned long type is not big enough to hold the sum of all those primes so it’s wrapping around.

    Whether it can hold the actual primes themselves I haven’t checked, but the answer in the next paragraph will solve that as well.

    You might want to try redefining bignum as a “larger” type like unsigned long long if available.

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

Sidebar

Related Questions

I will start by saying this is the first time I have done anything
If one needs to create an office website (that serves as a platform for
I have a mysql table with users and their weekly calendar. Every user can
It seems that a lot of small business people have a need for some
I know this may seem quite basic to geeks. But I want to make
I often see JavaScript code where a function may take in an options object
.NET is a language independent platform. But is it platform independent as well ?
I'm searching a software (web-based, php/mysql) that allows users to (anonymously/with registering) write suggestions
I'm struggling to work out how best to do what I think I want
I've looked up and down the SDK for this, but I can't find anything.

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.