sundeepblue
4/17/2014 - 6:00 PM

find all possible values of four variables when squared sum to N? A^2+B^2+C^2+D^2= N. Given a N, print out all possible combination for ABCD

find all possible values of four variables when squared sum to N? A2+B2+C2+D2= N. Given a N, print out all possible combination for ABCD

// reference: http://stackoverflow.com/questions/11732555/how-to-find-all-possible-values-of-four-variables-when-squared-sum-to-n

// naive solution
n = 3200724;
lim = sqrt (n) + 1;
for (a = 0; a <= lim; a++)
    for (b = 0; b <= lim; b++)
        for (c = 0; c <= lim; c++)
            for (d = 0; d <= lim; d++)
                if (a * a + b * b + c * c + d * d == n)
                    printf ("%d %d %d %d\n", a, b, c, d);
                    

// solution 2: discounting huge numbers of impossibilities at each level

#include <stdio.h>
int main(int argc, char *argv[]) {
    int n = atoi (argv[1]);
    int a, b, c, d, na, nb, nc, nd;
    int count = 0;

    for (a = 0, na = n; a * a <= na; a++) {
        for (b = 0, nb = na - a * a; b * b <= nb; b++) {
            for (c = 0, nc = nb - b * b; c * c <= nc; c++) {
                for (d = 0, nd = nc - c * c; d * d <= nd; d++) {
                    if (d * d == nd) {
                        printf ("%d %d %d %d\n", a, b, c, d);
                        count++;
                    }
                    tot++;
                }
            }
        }
    }
    printf ("Found %d solutions\n", count);
}

/*
For even more efficiency, you could only check those solutions where d >= c >= b >= a. That's because you could 
then build up all the solutions from those combinations into permutations (with potential duplicate removal where 
the values of two or more of a, b, c, or d are identical).
In addition, the body of the d loop doesn't need to check every value of d, just the last possible one.
Getting the results for 1,000,000 in that case takes under ten seconds rather than over six minutes:
*/

#include <stdio.h>
int main(int argc, char *argv[]) {
    int n = atoi (argv[1]);
    int a, b, c, d, na, nb, nc, nd;
    int count = 0;

    for (a = 0, na = n; a * a <= na; a++) {
        for (b = a, nb = na - a * a; b * b <= nb; b++) {
            for (c = b, nc = nb - b * b; c * c <= nc; c++) {
                for (d = c, nd = nc - c * c; d * d < nd; d++);
                if (d * d == nd) {
                    printf ("%4d %4d %4d %4d\n", a, b, c, d);
                    count++;
                }
            }
        }
    }
    printf ("Found %d solutions\n", count);
}

/*
the d loop can disappear altogether (since there's only one possible value of d (discounting negative numbers) 
and it can be calculated), which brings the one million case down to two seconds for me, and the ten million 
case to a far more manageable 68 seconds.
*/

#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
    int n = atoi (argv[1]);
    int a, b, c, d, na, nb, nc, nd;
    int count = 0;

    for (a = 0, na = n; a * a <= na; a++) {
        for (b = a, nb = na - a * a; b * b <= nb; b++) {
            for (c = b, nc = nb - b * b; c * c <= nc; c++) {
                nd = nc - c * c;
                d = sqrt (nd);
                if (d * d == nd) {
                    printf ("%d %d %d %d\n", a, b, c, d);
                    count++;
                }
            }
        }
    }
    printf ("Found %d solutions\n", count);
}