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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:31:20+00:00 2026-06-01T05:31:20+00:00

I’m working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet

  • 0

I’m working on problem 9 in Project Euler:

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

The following code I wrote uses Euclid’s formula for generating primes. For some reason my code returns “0” as an answer; even though the variable values are correct for the first few loops. Since the problem is pretty easy, some parts of the code aren’t perfectly optimized; I don’t think that should matter. The code is as follows:

#include <iostream>
using namespace std;

int main()
{
    int placeholder;    //for cin at the end so console stays open
    int a, b, c, m, n, k;
    a = 0;  b = 0;  c = 0;
    m = 0;  n = 0;  k = 0;  //to prevent initialization warnings
    int sum = 0;
    int product = 0;

    /*We will use Euclid's (or Euler's?) formula for generating primitive
     *Pythagorean triples (a^2 + b^2 = c^2): For any "m" and "n",
     *a = m^2 - n^2 ; b = 2mn ; c = m^2 + n^2 . We will then cycle through
     *values of a scalar/constant "k", to make sure we didn't miss anything.
     */

    //these following loops will increment m, n, and k,
    //and see if a+b+c is 1000. If so, all loops will break.
    for (int iii = 1; m < 1000; iii++)
    {
        m = iii;
        for (int ii = 1; n < 1000; ii++)
        {
            n = ii;
            for (int i = 1; k <=1000; i++)
            {
                sum = 0;
                k = i;
                a = (m*m - n*n)*k;
                b = (2*m*n)*k;
                c = (m*m + n*n)*k;
                if (sum == 1000) break;
            }
            if (sum == 1000) break;
        }
        if (sum == 1000) break;
    }
    product = a * b * c;

    cout << "The product abc of the Pythagorean triplet for which a+b+c = 1000 is:\n";
    cout << product << endl;
    cin >> placeholder;
    return 0;
}

And also, is there a better way to break out of multiple loops without using “break”, or is “break” optimal?


Here’s the updated code, with only the changes:

for (m = 2; m < 1000; m++)
    {
        for (int n = 2; n < 1000; n++)
        {
            for (k = 2; (k < 1000) && (m > n); k++)
            {
                sum = 0;
                a = (m*m - n*n)*k;
                b = (2*m*n)*k;
                c = (m*m + n*n)*k;
                sum = a + b + c;
                if ((sum == 1000) && (!(k==0))) break;
            }

It still doesn’t work though (now gives “1621787660” as an answer). I know, a lot of parentheses.

  • 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-01T05:31:21+00:00Added an answer on June 1, 2026 at 5:31 am

    The new problem is that the solution occurs for k = 1, so starting your k at 2 misses the answer outright.

    Instead of looping through different k values, you can just check for when the current sum divides 1000 evenly. Here’s what I mean (using the discussed goto statement):

          for (n = 2; n < 1000; n++)
            {
              for (m = n + 1; m < 1000; m++)
                {
                  sum = 0;
                  a = (m*m - n*n);
                  b = (2*m*n);
                  c = (m*m + n*n);
                  sum = a + b + c;
                  if(1000 % sum == 0)
                    {
                      int k = 1000 / sum;
                      a *= k;
                      b *= k;
                      c *= k;
                      goto done;
                    }
                }
            }
         done:
          product = a * b * c;
    

    I also switched around the two for loops so that you can just initialize m as being larger than n instead of checking every iteration.

    Note that with this new method, the solution doesn’t occur for k = 1 (just a difference in how the loops are run, this isn’t a problem)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from

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.