Home > Article > Backend Development > The sum of the first n natural numbers raised to fifth powers
Natural numbers are numbers starting from 1 and including all positive integers. The following article discusses two possible ways to calculate the sum of the fifth power of the first n natural numbers. This article discusses both approaches in detail and compares them in terms of efficiency and intuitiveness.
Problem Statement$\mathrm{1^5 2^5 3^5 4^5 5^5 … n^5}$ Until the nth item.
Example
Input: n = 3
Output: 276Explanation$\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}$Adding these terms, we get $ \mathrm{1^5 2^5 3^5 = 276}$
Therefore, the sum of the first 3 natural numbers is 276.
Input: n = 1
Output: 1Explanation$\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}$
So the sum of the first 1 natural number is 1.
Input: n = 11
Output: 381876Description
Intuitive method
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 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; }Output
The sum of the fifth powers of the first 3 natural numbers is: 276
Time complexity: O(n), because only one for loop is used inside the function sumOfFifthPower().
Space complexity: O(1), because no additional space is used.
alternative methodalgorithm
sumOfFifithPower().
// 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; }Output
The sum of the fifth powers of the first 3 natural numbers is: 276
Time complexity: O(1), since the answer is calculated in a single iteration using the direct formula.
Space complexity: O(1), because no additional space is required.
Compare the above methodsmethod 1 | Method 2 | ||
---|---|---|---|
O(n) | O(1) | ||
O(1) | O(1) | ||
More | The Chinese translation of | Lessis: | Less|
The Chinese translation of | Lessis: | LessMore |
The above is the detailed content of The sum of the first n natural numbers raised to fifth powers. For more information, please follow other related articles on the PHP Chinese website!