Code for checking for pythogrean triples.

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Code
Click For Summary
SUMMARY

The discussion focuses on optimizing code for finding Pythagorean triples using C programming. The original code contained duplicate results due to incorrect loop conditions. The solution proposed involves modifying the loop for variable 'b' to start from 'a + 1' and for 'c' to start from 'b + 1', effectively eliminating duplicates and improving performance. Additionally, the suggestion to replace the power function with multiplication enhances speed and avoids floating-point inaccuracies.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of Pythagorean triples
  • Knowledge of the gcd (greatest common divisor) function
  • Basic algorithm optimization techniques
NEXT STEPS
  • Implement and test the optimized loop conditions in C for Pythagorean triples
  • Explore the Fermat-Wiles Theorem and its implications on Pythagorean triples
  • Learn about efficient algorithms for finding integer solutions to equations
  • Investigate the use of integer arithmetic to avoid floating-point errors in programming
USEFUL FOR

Programmers, especially those working with C, algorithm enthusiasts, and anyone interested in mathematical programming challenges.

MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
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
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.
 
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);
           }
         }
       }
   }
}
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
14
Views
4K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
47
Views
5K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
1
Views
2K