Advertisement
CVSoft

Pythagorean Triples

Nov 4th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <conio.h>
  6.  
  7. typedef enum {false, true} bool;
  8.  
  9. bool isPerfectSquare(long int n)
  10. {
  11.     if (n % 7 == 3 || n % 7 == 5 || n % 11 == 8) {return false;} else
  12.     {
  13.         if (fabs(fmod(sqrt(n), 1.)) < 0.0001) {return true;}
  14.         return false;
  15.     }
  16. }
  17.  
  18. //Writing this stuff on my own is beyond my knowledge of pointers.
  19. int compare (const void * a, const void * b)
  20. {
  21.   return ( *(int*)a - *(int*)b );
  22. }
  23.  
  24. void main(void)
  25. {
  26.     int x;
  27.     int y;
  28.     int k;
  29.     int c = 0;
  30.     bool inter;
  31.     long int z;
  32.     int sl[40];
  33.     int dl[40][3];
  34.     int ol[40][3];
  35.     int t = (int)clock();
  36.     for(x = 1; x < 1024; x++)
  37.     {
  38.         for(y = 2; y < x; y++)
  39.         {
  40.             z = x*x + y*y;
  41.             if (isPerfectSquare(z))
  42.             {
  43.                 z = (int)sqrt(z);
  44.                 //printf("DING x=%d y=%d\n", x, y);
  45.                 inter = true;
  46.                 for (k = 2; k < (int)sqrt(z) + 1; k++)
  47.                 {
  48.                     if (x % k == 0 && y % k == 0 && z % k == 0) {inter = false; break;}
  49.                 }
  50.                 //if (!inter) {continue;} else {printf("DING DING x=%d y=&d");}
  51.                 if (inter)
  52.                 {
  53.                     //printf("DING DING x=%d y=%d z=%d n=%d\n", y, x, z, c);
  54.                     dl[c][0] = y;
  55.                     dl[c][1] = x;
  56.                     dl[c][2] = z;
  57.                     sl[c] = y;
  58.                     c++;
  59.                     if (c > 39) {goto done;}
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     done:;
  65.     // let's get some quicksort all up in 'dis
  66.     qsort(sl, 40, sizeof(int), compare);
  67.     // messily match and sort y and z vals; we're scrambled like eggs now
  68.     c = 0;
  69.     do
  70.     {
  71.         x = sl[c];
  72.         for(y = 0; y < 40; y++)
  73.         {
  74.             if (x == dl[y][0])
  75.             {
  76.                 // I'm sure this is better with pointers.
  77.                 ol[c][0] = dl[y][0];
  78.                 ol[c][1] = dl[y][1];
  79.                 ol[c][2] = dl[y][2];
  80.                 c++;
  81.             }
  82.         }
  83.     }
  84.     while(c < 40);
  85.     // time to display the hopefully correct output
  86.     t = (int)clock()-t;
  87.     for(c = 0; c < 20; c++)
  88.     {
  89.         printf("> x=%d  y=%d  z=%d\n", ol[c][0], ol[c][1], ol[c][2]);
  90.     }
  91.     printf("Execution time: %d ms\n", t);
  92.     _getch();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement