Question about one program

Feb 24, 2014 at 11:14am
Hi everyone i make one program about A Pythagorean triple: To show all Pythagorean triple (x2+y2=z2)smaller then n. Ex:If type 60 to give me all pythagorean triple smaller than 60 or "n"

I have this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <cmath>
using namespace std;

main()
{
int n,x,y,z;
cout<<"Number: ";
cin>>n;
for(x=1;x<=sqrt(n*n/2);x++)
{

   for(y=x;y<=n;y++)
   {
      z=sqrt(x*x*y*y);
      if((z<=n) && (z*z==x*x+y*y))
      cout<<"Pithagorean triple:"<<x<<""<<y;
      cout<<""<<z<<endl;
    }
  }
}
Feb 24, 2014 at 3:56pm
Hi Schwarz

What is the question about the program?

Try to give more information

Regards
Feb 24, 2014 at 5:37pm
My question is how to make when i type number "n" to give all Pythagorean triples smaller then number "n"
Feb 24, 2014 at 6:04pm
Be more specific when asking questions (which generally end with a '?'.) For instance, what result do you receive from your program which makes you believe it isn't working as you wish it to?

One obvious issue is that no return type is specified for main (it should be int,) so the code should fail to compile.
Last edited on Feb 24, 2014 at 7:18pm
Feb 24, 2014 at 7:15pm
I need to make program for finding all "Pythagorean Triple" smaller than some number "n". I dont now how to explain better than this.
Feb 24, 2014 at 7:29pm
Schwarz wrote:
I dont now how to explain better than this.
cire wrote:
... what result do you receive from your program which makes you believe it isn't working as you wish it to?


Here's another: What is meant by Pythagorean triples smaller than number "n". Is that a triple where every side must be smaller than n? Where the sum of the sides should be less than n? Where at least one side is smaller than n?

You realize line 18 is not governed by the if condition on line 16? Proper indentation in your code might help you see what's happening there.

1
2
3
        if ( z<=n   &&  z*z == x*x + y*y )
            cout << "Pithagorean triple:" << x << "" << y ;
        cout << "" << z << endl ;


And, I suspect those empty strings should contain at least a space.

Does line 15 in the OP look correct? Maybe one of those multiplications should be an addition.
Last edited on Feb 24, 2014 at 7:35pm
Feb 24, 2014 at 7:51pm
I saw this program from one book.
I need to find all pythagorean triples for some number
Last edited on Feb 24, 2014 at 8:13pm
Feb 24, 2014 at 8:13pm
Hi, I do not quite understand the question, I guess either print Pythagorean triples such that z <= n, or would be, print the first 60 Pythagorean triples?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
int n = 60, i;
    
    int x =  3;
    int y =  4;
    int z =  5;
    
    while( z <= n )
    {
      std::cout<< x <<" " << y <<" " <<z <<std::endl;
       x *= 2;
       y *= 2;
       z *= 2;      
    }
...


@cronopio
Feb 24, 2014 at 8:32pm
No i just use 60 as exemple, for any number that i type.
Feb 25, 2014 at 7:25am
Somebody to help ?
Feb 25, 2014 at 7:47am
If you bothered to answer the questions previously posed, or addressed the code concerns previously raised with updated code, someone might be willing to match your effort.
Feb 25, 2014 at 7:47am
Isn't a Pythagorean Triplet when a < b < c and a2 + b2 = c2?

This would mean the largest possible for c to be would 7.


this is probably a pretty bad approach (brute force):

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
#include <iostream>
#include <cmath> //sqrt if you wish to change value of n
int main()
{
	int a , b , c;
	//a < b < c
	//a^2 + b^2 = c^2
	//n = 60 so sqrt is about 7
	const int sq_rt = 7;
	
	//if you wish to change the range:
	//const int n = 1000;
	//const int sq_rt = sqrt( n );
	
	for( c = sq_rt; c > 4; --c ) //worst case c == 5 when b == 4 and a == 3
	{
		for( b = c - 1; b > 3; --b ) //worst case b == 4  when a == 3
		{
			for( a = b - 1; a > 2; --a ) //worst case a == 3
			{
				if( a * a + b * b == c * c )
				{
					std::cout << "Triplet: a = " << a << " b = " << b << " c = " << c << std::endl;
					std::cout << "Triplet value = " << c * c << std::endl;
				}
			}
		}
	}
	return 0;
}


http://ideone.com/r3uhPh

or are you saying n is a + b + c?

This is from project euler a long time ago but can show you the basics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

//problem 9:
//find the product of a + b + c = 1000 where a < b < c && a^2 + b^2 = c^2


int main()
{
    int a , b , c;

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


http://ideone.com/QuIPZE

[edit]forgot link to ideone
[edit2]Another thing to mention if you actually listed a formula of what you are trying to do it would help. I am not positive what you are trying to do with n.
Last edited on Feb 25, 2014 at 7:53am
Feb 25, 2014 at 11:26am
n is variable for number that i need to type.
Feb 25, 2014 at 3:49pm
Hi Schwarz

One thing you could do is construct yourself the Pythagorean triplet and run it in a loop until the condition that it should be smaller than "n" is not valid any more

You can find information on how to construct the triplets in google, I have the feeling that your "question" is "make the program for me" and in this way you will have no benefits but the code

You are going in the right path using loops, but your conditions are not good.

Best Regards
Feb 25, 2014 at 5:57pm
n is variable for number that i need to type.


One could not have guessed....
Feb 25, 2014 at 6:05pm
I am begginer and i need this progam for school.
Topic archived. No new replies allowed.