Code for checking for pythogrean triples.

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion revolves around writing code to find Pythagorean triples while ensuring the condition a < b < c is met to avoid duplicates. The initial code lacks this condition, resulting in repeated triples. A suggestion is made to adjust the for loops so that b starts from a + 1 and c starts from b + 1, which would streamline the process and enhance efficiency. Additionally, it is recommended to use multiplication instead of exponentiation to prevent floating-point issues and improve performance. The conversation highlights the importance of adhering to mathematical principles, such as Fermat's Last Theorem, which states there are no solutions for n > 2. The revised code structure aims to optimize the search for valid triples while maintaining accuracy.
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);
           }
         }
       }
   }
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top