Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include <conio.h>
- typedef enum {false, true} bool;
- bool isPerfectSquare(long int n)
- {
- if (n % 7 == 3 || n % 7 == 5 || n % 11 == 8) {return false;} else
- {
- if (fabs(fmod(sqrt(n), 1.)) < 0.0001) {return true;}
- return false;
- }
- }
- //Writing this stuff on my own is beyond my knowledge of pointers.
- int compare (const void * a, const void * b)
- {
- return ( *(int*)a - *(int*)b );
- }
- void main(void)
- {
- int x;
- int y;
- int k;
- int c = 0;
- bool inter;
- long int z;
- int sl[40];
- int dl[40][3];
- int ol[40][3];
- int t = (int)clock();
- for(x = 1; x < 1024; x++)
- {
- for(y = 2; y < x; y++)
- {
- z = x*x + y*y;
- if (isPerfectSquare(z))
- {
- z = (int)sqrt(z);
- //printf("DING x=%d y=%d\n", x, y);
- inter = true;
- for (k = 2; k < (int)sqrt(z) + 1; k++)
- {
- if (x % k == 0 && y % k == 0 && z % k == 0) {inter = false; break;}
- }
- //if (!inter) {continue;} else {printf("DING DING x=%d y=&d");}
- if (inter)
- {
- //printf("DING DING x=%d y=%d z=%d n=%d\n", y, x, z, c);
- dl[c][0] = y;
- dl[c][1] = x;
- dl[c][2] = z;
- sl[c] = y;
- c++;
- if (c > 39) {goto done;}
- }
- }
- }
- }
- done:;
- // let's get some quicksort all up in 'dis
- qsort(sl, 40, sizeof(int), compare);
- // messily match and sort y and z vals; we're scrambled like eggs now
- c = 0;
- do
- {
- x = sl[c];
- for(y = 0; y < 40; y++)
- {
- if (x == dl[y][0])
- {
- // I'm sure this is better with pointers.
- ol[c][0] = dl[y][0];
- ol[c][1] = dl[y][1];
- ol[c][2] = dl[y][2];
- c++;
- }
- }
- }
- while(c < 40);
- // time to display the hopefully correct output
- t = (int)clock()-t;
- for(c = 0; c < 20; c++)
- {
- printf("> x=%d y=%d z=%d\n", ol[c][0], ol[c][1], ol[c][2]);
- }
- printf("Execution time: %d ms\n", t);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement