首页  >  文章  >  后端开发  >  前n个自然数的五次幂之和

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

PHPz
PHPz转载
2023-09-11 14:45:091224浏览

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

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

问题陈述

这个问题的目的是计算前n个自然数的算术和,所有数都被提升到它们的五次方,即

$mathrm{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}$

因此,前3个自然数的和为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

  • for (i从1到n)

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

  • 返回总和

示例

该程序计算每个数字的五次方,并在每次迭代中使用 for 循环将其添加到现有总和中,该循环在函数 sumOfFifthPower() 中实现了 n 次。

// 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{displaystylesumlimits_{k=1}^n :k^5=frac{1}{12}(2n^6+6n^5+5n^4−n^ 2)}$$

算法

函数main()

  • 初始化n。

  • 函数调用 sumOfFifthPower()。

  • 打印总和。

函数 sumOfFifthPower(int n)

  • 初始化 sum = 0

  • 总和 = ((2 * pow(n,6)) + (6 * pow(n,5) + (5 * pow(n,4) - (pow(n,2)) / 12

  • 返回总和

示例

该程序通过将n的值代入一个数学公式来计算总和,该公式计算了函数sumOfFifithPower()中前n个自然数的五次幂的总和。

// 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),因为不需要额外的空间。

比较上述方法

的中文翻译为: 的中文翻译为:
标准 方法1 方法2
时间复杂度 O(n) O(1)
空间复杂度 O(1) O(1)
直观性 更多 LessLess
效率 LessLess 更多

结论

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

以上是前n个自然数的五次幂之和的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除