C程序查找系列1 ^ 4 + 2 ^ 4 + 3 ^ 4 + ... + n ^ 4的和

时间:2014-08-19 17:26:52

标签: c sum

我完成了系列1^3 + 2^3 + 3^3 + … + n^

的总和
#include <stdio.h>
#include <math.h>
int main()
 {
    int number, i;
    int sum = 0;

    printf("Enter the maximum values of series n: ");
    scanf("%d", &number);
    sum = pow(((number * (number + 1) ) / 2),2);
    printf("Sum of the series : ");
    for (i = 1; i <= number; i++)
    {
        if (i != number)
            printf("%d^3 + ", i);
        else
            printf("%d^3 = %d ", i, sum);
    }
    return 0;
  }

但我没有获得第四种力量。对第四种力量有任何帮助吗?

样本输入为2,样本输出为17

数学上1 ^ 4 + 2 ^ 4 = 17(对于4次幂)

2 个答案:

答案 0 :(得分:4)

如上所述,使用蛮力。请注意int类型的最大值限制,必要时使用unsigned long long

#include <stdio.h>
#include <math.h>
int main()
{
    int number, i;
    int sum = 0;

    printf("Enter the maximum values of series n: ");
    scanf("%d", &number);
    printf("Sum of the series : ");
    for (i = 1; i <= number; ++i)
    {
        sum += pow(i,4);

        if (i < number)
        {
            printf("%d^4 + ", i);
        }
        else
            printf("%d^4 = %d ", i, sum);            
    }
    return 0;
}

此版本为您提供了处理这两种情况的灵活性:

#include <stdio.h>
#include <math.h>
int main()
{
    int numTerms, power, i;
    int sum = 0;

    printf("Enter number of terms in the series n: ");
    scanf("%d", &numTerms);
    printf("\nEnter power of the series n: ");
    scanf("%d", &power);
    printf("\nSum of the series: ");
    for (i = 1; i <= numTerms; ++i)
    {
        sum += pow(i, power);

        if (i < numTerms)
        {
            printf("%d^%d + ", i, power);
        }
        else
            printf("%d^%d = %d \n", i, power, sum);
    }
    return 0;
}

答案 1 :(得分:4)

1^4 + 2^4 + 3^4 + ... + n^4的公式为(6n^5 + 15n^4 + 10n^3 - n) / 30

翻译为C:

#include <stdio.h>
#include <limits.h>
#include <math.h>

int fn(int n, int power)
{
    double f = 0.0;
    int i;

    if (n < 0) 
        f = -fn(-n, power); /* negatives */
    else switch (power) {
        case 0:
            f = 1.0;
            break;
        case 1:
            f = (pow(n, 2) + n) / 2;
            break;
        case 2:
            f = (2 * pow(n, 3) + 3 * pow(n, 2) + n) / 6;
            break;
        case 3:
            f = (pow(n, 4) + 2 * pow(n, 3) + 3 * pow(n, 2)) / 4;
            break;
        case 4:
            f = (6 * pow(n, 5) + 15 * pow(n, 4) + 10 * pow(n, 3) - n) / 30;
            break;
        case 5:
            f = (2 * pow(n, 6) + 6 * pow(n, 5) + 5 * pow(n, 4) - pow(n, 2)) / 12;
            break;
        default:
            if (power < 0) break;
            /*
                fn(28,6) overflows INT_MAX
                This means 28 iterations in the worst case, use brute force:
            */
            for (i = 1; i <= n; i++) {
                f += pow(i, power);
                if (f > INT_MAX) break;
            }
    }
    if (f < INT_MIN || f > INT_MAX) return 0;
    return (int)f;
}

int main(void)
{
    int n, power, result;

    printf("Enter number: ");
    scanf("%d", &n);
    printf("Enter power:  ");
    scanf("%d", &power);
    if ((result = fn(n, power)) == 0 && n != 0) {
        fprintf(stderr, "Error computing fn(%d,%d)\n", n, power);
    } else {    
        printf("Sum of series: %d\n", result);
    }
    return 0;
}

更多信息:Computing sum of series ^1 ... ^10

enter image description here