首頁  >  文章  >  後端開發  >  前n個自然數的五次冪之和

前n個自然數的五次冪之和

PHPz
PHPz轉載
2023-09-11 14:45:091226瀏覽

前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{\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

  • 總和= ((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刪除