C++ 前n个自然数的五次幂之和

C++ 前n个自然数的五次幂之和

自然数是从1开始,并包括所有正整数。本文讨论了计算前n个自然数的五次幂之和的两种可能方法。文章详细讨论了这两种方法,并从效率和直观性方面进行了比较。

问题陈述

这个问题的目的是计算前n个自然数的算术和,其每个数都被提升到五次幂,即

1^5 + 2^5 + 3^5 + 4^5 + 5^5 + … + n^5,直到第n项。

示例

由于n是一个自然数,它的值不能小于1。

Input: n = 3
Output: 276

解释

\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}

\mathrm{2^5 = 2 * 2 * 2 * 2 * 2 = 32}

\mathrm{3^5 = 3 * 3 * 3 * 3 * 3 = 243}

将这些项相加,我们得到,\mathrm{1^5 + 2^5 + 3^5 = 276}

因此前三个自然数的和为276。

Input: n = 1
Output: 1

解释

\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}

因此,前1个自然数的和是1。

Input: n = 11
Output: 381876

解释

\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}

\mathrm{2^5 = 2 * 2 * 2 * 2 * 2 = 32}

…..

\mathrm{11^5 = 11 * 11 * 11 * 11 * 11 = 161051}

将这些项相加,得到 \mathrm{1^5 + 2^5 + 3^5 + … + 11^5 = 381876}

因此,前11个自然数的和为 381876。

直观方法

  • 使用迭代的循环逐个计算每个数的五次方。

  • 创建一个变量,在循环的每次迭代后存储和。

  • 显示答案。

步骤

主函数 main()

  • 初始化 n。

  • 调用函数 sumOfFifthPower()。

  • 打印和。

函数 sumOfFifthPower(int n)

  • 初始化 sum = 0

  • 对于 i 从 1 到 n

    • sum = sum + (pow(i,5)
  • 返回 sum

示例

该程序通过使用一个循环将每个数的五次方加到现有的和中,来计算每个数字的五次方。

// A C++ program to find the sum of the first n natural numbers, all raised to their fifth power.
#include <iostream>
#include <cmath>
using namespace std;
// This function calculates the summation of fifth powers of the first // n natural numbers and stores
// it in the variable sum
int sumOfFifthPower(int n){
   int sum = 0;
   for (int i = 1; i <= n; i++)    {

      // calculate fifth power of i and add it to sum
      sum = sum + pow(i, 5);
   }
   return sum;
}

// main function
int main(){
   int n = 3;
   int ans; // to store final result
   ans = sumOfFifthPower(n); // function call
   cout << "The sum of the fifth powers of the first " << n << " natural numbers is: ";
   cout << ans; // Display the final result
   return 0;
}

输出

The sum of the fifth powers of the first 3 natural numbers is: 276

时间和空间的分析

时间复杂度:O(n) ,因为在函数sumOfFifthPower()中只使用了一个for循环。

空间复杂度:O(1) ,因为没有使用额外的空间。

其他方法

  • 使用数学公式计算每个数的五次方的和。

  • 显示结果。

公式

$$
\mathrm{\displaystyle\sum\limits_{k=1}^n:k^5=\frac{1}{12}(2n^6+6n^5+5n^4−n^2)}
$$

算法

函数main()

  • 初始化n。

  • 调用函数sumOfFifthPower()。

  • 打印结果。

函数sumOfFifthPower(int n)

  • 初始化sum = 0。

  • sum = ((2 * pow(n,6)) + (6 * pow(n,5) + (5 * pow(n,4) – (pow(n,2)) / 12。

  • 返回sum。

示例

该程序通过将n的值代入数学公式来计算第n个自然数的五次方的和,在函数 sumOfFifthPower() 中。

// A C++ program to find the sum of the first n natural numbers, all raised to their fifth power.
#include <iostream>
#include <cmath>
using namespace std;

// This function calculates the summation of fifth powers of the first // n natural numbers and stores it in the variable sum
int sumOfFifthPower(int x){
   int sum = 0;
   sum = ((2 * pow(x,6)) + (6 * pow(x,5)) + (5 *pow(x,4)) - (pow(x,2))) / 12; 
   return sum;
}

// main function
int main(){
   int n = 3;
   int ans; // to store final result
   ans = sumOfFifthPower(n); // function call
   cout << "The sum of the fifth powers of the first " << n << " natural numbers is: ";
   cout << ans; // Display the final result
   return 0;
}

输出

The sum of the fifth powers of the first 3 natural numbers is: 276

时间和空间分析

时间复杂度:O(1) ,因为答案是通过使用一个直接公式在单次迭代中计算得出的。

空间复杂度:O(1) ,因为不需要额外的空间。

比较上述方法

Criteria 方法1 方法2
Time Complexity O(n) O(1)
Space Complexity O(1) O(1)
Intuitiveness More Less
Efficiency Less More

结论

本文讨论了两种方法来寻找前n个自然数的五次方和。它还提供了方法的概念、算法使用、C++程序解决方案以及每种方法的复杂性分析。可以观察到,虽然第一种方法的时间复杂度更高,但它更直观。另一方面,第二种方法使用直接的数学公式以O(1)的时间和空间高效地解决了问题。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程