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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:43:27+00:00 2026-05-28T07:43:27+00:00

I am trying to solve Project Euler Problem 88 , and I did it

  • 0

I am trying to solve Project Euler Problem 88, and I did it without too much effort; however, I find that some seemingly irrelevant code in my program is affecting the result. Here’s my complete code (it’s not short, but I cannot locate the error. I believe it would be obvious to more experienced eyes, so please read my description first):

#include <iostream>
#include <set>

using namespace std;

bool m[24001][12001];
bool p[24001]; // <------------ deleting this line will cause error in result!
long long answer[12001];

int main() {
  long long i;
  long long j;
  long long l;
  set<long long> all;
  long long s = 0;
  for (i = 0; i <= 24000; i++) {
        for (j = 0; j <= 12000; j++) {
      m[i][j] = false;
    }
  }
  m[1][1] = true;
  for (i = 2; i <= 24000; i++) {
    m[i][1] = true;
    for (j = 2; (j <= i) && (i * j <=24000); j++) {
      for (l = 1; l <= i; l++) {
    if (m[i][l]) {
      m[i * j][l + 1 + (i * j) - i - j] = true;
    }
      }
    }
  }
  for (i = 0; i <= 24000; i++) {
    for (j = 0; j <= 12000; j++) {
      if (m[i][j] && (answer[j] == 0)) {
    answer[j] = i;
      }
    }
  }
  for (i = 2; i <= 12000; i++) {
    cout << answer[i] << endl;
    all.insert(answer[i]);
  }
  cout << all.size() << endl;
  for (set<long long>::iterator it = all.begin(); it != all.end(); it++) {
    //cout << *it << endl;
    s += *it;
  }
  cout << s << endl;
}

With the “useless” bool array, all the answers are right, between 0 and 24000; but without it, some answers in the middle got corrupted and become very large numbers.

I am completely confused now; why would that unused array affect the middle of the answer array?

Thanks and sorry for the long code! I will be grateful if someone could edit the code into a better example, I simply son’t know what is with the code.

  • 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-28T07:43:28+00:00Added an answer on May 28, 2026 at 7:43 am

    As requested, adding this to an answer.

    You are definitely writing beyond the bounds of the array m somewhere, when the unused array p exists, m overwrites in to its contents which doesn’t affect the answer array but once p is removed the overwriting happens in to answer array showing up the problems.

    Overwriting beyond the bounds of the array is an Undefined Behavior and it causes your program to be ill-formed. With Undefined Behavior all safe bets are off and any behavior is possible. While your program may work sometimes or crash sometimes or give incorrect results.Practically, Anything is possible and the behavior may or even may not be explainable.

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

Sidebar

Related Questions

I have been trying to solve Project Euler's problem #59 for a while, and
I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I
I'm trying to solve problem 50 on Project Euler . Don't give me the
I'm trying to solve Problem #5 in Project Euler. The code works for the
I'm trying to solve problem 14 on project euler, and my initial brute force
I am trying to solve Project Euler problem #12: The sequence of triangle numbers
I'm trying to solve the 8th problem of the project Euler and I'm stuck
For a little context, i'm trying to solve Project Euler problem 31 using excellent
I'm currently trying to solve problem 18 of project Euler . The goal is:
I'm trying to solve Problem 41 of project Euler in Java, by counting the

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.