/* jiw Re: Subject: a^2+b^2 = c^2+d^2 Date: 17 Jul 2006 22:54:52 -0700 From: titus_piezas@yahoo.com Newsgroups: sci.math.num-analysis, sci.math Counting the number of positive-integer a,b,c,d tuples, with a,b coprime and c,d coprime, a # c, such that a^2+b^2 = c^2+d^2 < k^2, for various k, or in this program version, a^3+b^3 = c^3+d^3 < k^3. Copyright 2006 James Waldby. Offered without warranty under GPL terms as at http://www.gnu.org/licenses/gpl.html */ #define zz fprintf(stderr,"%s@%3d \n",__FILE__,__LINE__); #include #include #include #include //================================================ #define HITE 3 #if HITE==2 #define POWER(x) x*x #define ROOT(x) sqrt(x) #endif #if HITE==3 #define POWER(x) x*x*x #define ROOT(x) cbrt(x) #endif #if HITE==4 #define POWER(x) x*x*x*x #define ROOT(x) sqrt(sqrt(x)) #endif #define KMULTI 1 #define KDELT 20 #define KONE 20 #define ZMAX 40000001 #define ZHAT ((7*ZMAX)/5) #define DEB 0 //================================================ // gcd(a,b) by Euclid's algorithm int gcd(int a, int b) { int r = a%b; while (r) { a=b; b=r; r=a%b; } return b; } //================================================ int main(int argc, char *argv[]) { int a, b, c, d, g, gab, h; int i, ii, j, k, t, u, vnext, z; int *cl, *cv; if(DEB)printf ("\nbegin\n"); printf ("\npower(2)=%d, root(4096)=%f\n", POWER(2),ROOT(4096)); cl = malloc(ZHAT*sizeof(int)); cv = malloc(ZHAT*sizeof(int)); memset (cl, 0, ZHAT*sizeof(int)); // Zero the links array vnext = ZMAX; for (i=1; ; ++i) { ii = POWER(i); if (ii > ZMAX) break; for (j=i; ; ++j) { t = ii + POWER(j); if (t > ZMAX) break; if (cl[t]) { // Add [i j] to list t cv[vnext] = i; cl[vnext] = cl[t]; cl[t] = vnext; ++vnext; if (vnext >ZHAT) { printf ("\nOverflowing arrays, vnext = %d\n", vnext); exit (0); } } else { // Begin list t with [i j] cl[t] = t; cv[t] = i; }; } } // For various z, count cases where a^n+b^n=c^n+d^n with (a,b,c,d)=1 h = 1; j = 0; k=KONE; z=POWER(k); do { for (; h