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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:39:27+00:00 2026-05-14T19:39:27+00:00

I made a program that returns the product a b c where a,b,c are

  • 0

I made a program that returns the product abc where a,b,c are pythagorean triples and add up to 1000. The program does output the correct answer but does it twice. I was curious as to why this is so. After playing around with it a bit I found out that it prints out when a = 200 b = 375 c = 425. And once again when a = 375 b = 200 c = 425.

bool isPythagTriple(int a, int b, int c);

int main()
{

    for(int a = 1; a < 1000; a++)
    {
        for(int b = 1; b < 1000; b++)
        {
            for(int c = 1; c < 1000; c++)
            {
                if( ((a+b+c)==1000) && isPythagTriple(a,b,c) )
                {
                    cout << a*b*c << " ";
                    break;
                }
            }
        }
    }

    return 0;
}

bool isPythagTriple(int a, int b, int c)
{
    if( (a*a)+(b*b)-(c*c) == 0 )
        return true;
    else
        return false;
}
  • 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-14T19:39:28+00:00Added an answer on May 14, 2026 at 7:39 pm

    Break, in this case, will only break out of the c loop, not the b and a ones.

    A quick fix is to ensure you don’t get repeats by starting each variable greater than or equal to the previous (so b is never less than a and c is never less than b).

    In addition, you can actually get rid of the c loop altogether since there’s only one value of c that is valid for a given a,b pair (unless a + b + c > 1000 in which case there are none). I would try something like:

    for (int a = 1; a < 1000; a++) {
        for (int b = a; b < 1000; b++) {
            int c = 1000 - a - b;
            if (c >= b) {
                if (isPythagTriple (a,b,c)) {
                    cout << a << " " << b << " " << c << " " << a*b*c << std::endl;
                }
            }
        }
    }
    

    The overall effect of that is to reduce the total loop count from a billion (short scale) to about half a million hence reducing it by about 99.95% – that should be a tiny bit faster 🙂


    And potentially making it faster with Jerry Coffin’s suggestion as well (and an inline suggestion to the compiler), a full program:

    #include <iostream>
    
    inline bool isPythagTriple(int a, int b, int c) {
        return a * a + b * b == c * c;
    }
    
    int main() {
        for(int a = 1; a < 1000; a++) {
            for(int b = a; b < 1000; b++) {
                int c = 1000 - a - b;
                if (c >= b) {
                    if (isPythagTriple(a,b,c)) {
                        std::cout << a << " " << b << " " << c << " "
                            << a*b*c << std::endl;
                    }
                }
            }
        }
        return 0;
    }
    

    which takes 0.004 seconds on average (system + user) on my box, with the original taking about 2.772 seconds on average (ten samples each). Not that it really matters unless you’re running it many, many times, of course.

    The output of that code is, as expected:

    200 375 425 31875000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 393k
  • Answers 393k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer mysql_fetch_array() gets a single row at a time from the… May 15, 2026 at 2:09 am
  • Editorial Team
    Editorial Team added an answer You could use varargs: public void DataProviderExample(String user, String pwd,… May 15, 2026 at 2:09 am
  • Editorial Team
    Editorial Team added an answer I think you might be looking at this backwards. In… May 15, 2026 at 2:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.