c_cpp 当平方和为N时,找到四个变量的所有可能值? A ^ 2 + B ^ 2 + C ^ 2 + D ^ 2 = N.给定N,打印出ABCD的所有可能组合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 当平方和为N时,找到四个变量的所有可能值? A ^ 2 + B ^ 2 + C ^ 2 + D ^ 2 = N.给定N,打印出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);
}

以上是关于c_cpp 当平方和为N时,找到四个变量的所有可能值? A ^ 2 + B ^ 2 + C ^ 2 + D ^ 2 = N.给定N,打印出ABCD的所有可能组合的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 用和N打印连续数字的所有可能总和

python计算100以内所有奇数的和

R语言单因素、多因素方差分析ANOVA analysis of variance

EXCEL中R的平方值代表啥意思?

python生成卡方分布

24.用牛顿迭代法求平方根