Code for checking for pythogrean triples.

  • Thread starter MathematicalPhysicist
  • Start date
  • Tags
    Code
In summary, the conversation is about writing a code to search for Pythagorean triples. The fault in the code is that it does not have a condition for a<b<c, resulting in duplicates. Suggestions were given to place the condition in the for loops, specifically for b=a+1 to the max, and for c=b+1 to the max. It was also suggested to use multiplication instead of power for faster results and to consider the Fermat-Wiles Theorem for cases where n is greater than 2.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
i was asked to write a code that search for pythogrean triples, my fault in my code is that i don't know where to write the codnition that a<b<c, cause as of now runs duplicates of pyhtogrean's triples.
here's the code:
Code:
main()
{
  int n,max,a,b,c;
 printf("insert the degree you wish to check for n-th pythogrean triplets\n");
 scanf("%d", &n);
 printf("insert the maximal number you want to check for\n");
 scanf("%d", &max);
 if(max>2){
   
   for(a=1;a<=max;a++)
     {
       for(b=2;b<=max;b++)
         {
   for(c=3;c<=max;c++)
     {
       if((pow(a,n)+pow(b,n)==pow(c,n))&& gcd(a,b)==1){
         f++;
         printf("the triplet is: %d, %d, %d \n", a,b,c);
       
       
     }
         }
     }
 }
}
any hints as to where to place this condition?
 
Technology news on Phys.org
  • #2
In the for loops, forsooth. If b should be greater than a, why not run the for loop for b=a+1 to the max, and likewise for c.
 
  • #3
Moo Of Doom said:
In the for loops, forsooth. If b should be greater than a, why not run the for loop for b=a+1 to the max, and likewise for c.

Yes, this will make the code perhaps 4 times faster in addition to removing duplicates.

To make it faster yet (and ward off potential floating-point problems) you should multiply rather than power:

Code:
main()
{
  int n,max,a,b,c;
 printf("insert the degree you wish to check for n-th pythogrean triplets\n");
 scanf("%d", &n);
 printf("insert the maximal number you want to check for\n");
 scanf("%d", &max);
 if(n > 2) {
   /* There are no solutions by the Fermat-Wiles Theorem */
 }
 elseif(max>2 && n == 2){
   for(a=1;a<max;a++) {
       for(b=a+1;b<max;b++) {
         for(c=b+1;c<=max;c++) {
           if(a*a+b*b==c*c && gcd(a,b)==1) {
             f++;
             printf("the triplet is: %d, %d, %d \n", a,b,c);
           }
         }
       }
   }
}
 

1. What is a Pythagorean triple?

A Pythagorean triple is a set of three positive integers, (a, b, c), that satisfy the Pythagorean theorem, a² + b² = c². This means that the square of the length of the longest side (c) is equal to the sum of the squares of the other two sides (a and b).

2. How do you check for Pythagorean triples in code?

To check for Pythagorean triples in code, you can use the Pythagorean theorem formula (a² + b² = c²) and iterate through all possible combinations of three positive integers to see if they satisfy the equation.

3. Can you give an example of a Pythagorean triple?

One example of a Pythagorean triple is (3, 4, 5). This means that a = 3, b = 4, and c = 5, and when plugged into the Pythagorean theorem formula, it results in 3² + 4² = 5², which is true.

4. Are there any special properties of Pythagorean triples?

Yes, there are several special properties of Pythagorean triples. One is that the greatest common divisor of a, b, and c must be 1. Another is that all primitive Pythagorean triples (where a, b, and c are coprime) can be generated using the formula a = m² - n², b = 2mn, and c = m² + n², where m and n are positive integers with m > n.

5. How can Pythagorean triples be used in real-world applications?

Pythagorean triples have many real-world applications, such as in geometry and engineering. They can be used to calculate the lengths of sides in right triangles, which is useful in construction and architecture. They are also used in navigation, as the Pythagorean theorem can be applied to calculate distances and angles. Additionally, Pythagorean triples have applications in cryptography and number theory.

Similar threads

  • Programming and Computer Science
Replies
9
Views
997
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
894
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
3
Views
992
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top