Empty Output in Pythagorean Triple program

I am given the sum of a pythagorean triple and must deduce what the hypotenuse squared is. But my output is empty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
int pythagoras(int k, int intsum)
{
    for (long double m=2; m < 99999999999999; m++) 
        {
            for (long double n = 1; n < 99999999999999; n++) 
            {
                while (m > n)  
                {  //Euler's Formula
                   int a = k*((m*m) - (n*n));
                   int b = k*((2*m) * n);
                   int c = k*((m*m) + (n*n));
                   int sum = a + b + c;
                    if (sum == intsum) 
                    {
                        cout<<a*a+b*b<<' ';
                        break;
                    }
                }
            }
        }
        return 0;
}
int main()
{
        int inputs, intsum;
       cin>>inputs;
 for (int i=0; i<inputs; i++)
   {
       cin>>intsum;
       for (int k=0; k<99999999999999; k++ )
       pythagoras(k, intsum);
    }
} 


Input:
7
17983724
21590068
14933564
16180218
21541286
20830558
23033462
(first number is number of inputs)
You have several problems.

The first is that you should be using long long int for all your data types.

The second is that you have a while instead of an if on line 9.

The third is that you are doing an expensive computation approximately 1×1042 times -- which takes forever. There is no need to check all possibilities for all (k,m,n > intsum)!

Also, you are ignoring the properties of Euclid's formula and performing computations every time. Don't do that.

You can significantly reduce your runtime (and then see output) by using a more careful solution. See this Stack Overflow answer for a good algorithm:
http://stackoverflow.com/a/2835053/2706707

You'll need a function that generates all the divisors of a number (stick it in a vector or something).

Hope this helps.


[edit]
By the way, all your input numbers have solutions that:
- are composed of three 7-digit numbers
- k = 1
- m,n < 3000
If you simply reduce your loops to those values you'd see output.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    int sum = 1000;
    int c = 0;
    
    for (int a = 1; a <= sum; a++)
    {
        for (int b = a; b <= sum - a; b++)
        {
            c = sum - a - b;            
            
            if ( a*a + b*b - c*c == 0)
                std::cout << a << ',' << b << ',' << c << "\t Sum = " << a + b + c << std::endl;
        }
    }
    
    return 0;
}


200,375,425	 Sum = 1000
Last edited on
@kemort
Have you tried running that with the OP's input?
closed account (48T7M4Gy)
It does run in the shell using the OP input. Try it yourself by substituting the values for sum. Trouble is the answers aren't reliable. If you use long or long long the program needs to run for a "while", as you have already indicated.

sum = 1000 has special significance elsewhere.
It does run in the shell using the OP input. Try it yourself by substituting the values for sum.

LOL, no it doesn't. You even agree:

Trouble is the answers aren't reliable.

The answers aren't just unreliable, they're nonsense. This is because you are incurring overflow.

If you use long or long long the program needs to run for a "while", as you have already indicated.

Yes, it runs a long while because you are brute-forcing through every (a,b,c).
Don't do that.

sum = 1000 has special significance elsewhere.

Special significance in that...

     - it is a commonly asked value for this problem? (due to Project Euler?)
     - it is small and easy to compute?
     - it has only one sorted, primal solution?
     - magical three zeros? Unicorns? Rainbows?

For comparison, here's the OP's original solution (cleaned-up as per my suggestions), which actually works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// http://www.cplusplus.com/forum/beginner/192130/
// OP's solution, fixed.

#include <iostream>
using namespace std;

void pythagoras(long long k, long long intsum)
{
    for (long long m=2; m < 9999; m++) 
    {
        for (long long n = 1; n < 9999; n++) 
        {
            if (m > n)  
            {   //Euclid's Formula
                long long a = k*((m*m) - (n*n));
                long long b = k*((2*m) * n);
                long long c = k*((m*m) + (n*n));
                long long sum = a + b + c;
                if (sum == intsum) 
                {
                    // It is possible that there is more than on solution
                    // (Try 60 as input, for example)
                    cout << intsum << " : (" << a << ", " << b << ", " << c << ")\n";
                    break;
                }
            }
        }
    }
}

int main()
{
    long long inputs, intsum;
    cin>>inputs;
    for (long long i=0; i<inputs; i++)
    {
        cin>>intsum;                    // Your choice of k matters.
        for (long long k=1; k<2; k++ )  // With k==1 we only find primitive triples,
        {                               // which is all you should output anyway.
            pythagoras(k, intsum);      // (You might consider getting rid of the loop.)
        }
    }
} 
17983724 : (4901035, 5623332, 7459357)
21590068 : (5437707, 7160876, 8991485)
14933564 : (2267083, 6130356, 6536125)
16180218 : (4298409, 5163400, 6718409)
21541286 : (4984525, 7528068, 9028693)
20830558 : (4918437, 7195916, 8716205)
23033462 : (7359781, 6108900, 9564781)

Hope this helps.
thanks guys.
Topic archived. No new replies allowed.