- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The Sum of the Fifth Powers of the First n Natural Numbers
Natural numbers are numbers that start from 1 and include all the positive integers. The following article discusses two possible approaches to compute the sum of the fifth powers of the first n natural numbers. The article discusses the two approaches in detail and compares them with regards to efficiency and intuitiveness.
Problem Statement
The purpose of this problem is to compute the arithmetic sum of the first n natural numbers, all raised to their fifth power i.e.
$\mathrm{1^5 + 2^5 + 3^5 + 4^5 + 5^5 + … + n^5}$ till the nth term.
Examples
Since n is a natural number, its value cannot be less than 1.
Input: n = 3
Output: 276
Explanation
$\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}$
On adding the terms, we get, $\mathrm{1^5 + 2^5 + 3^5 = 276}$
Hence the sum of the first 3 natural numbers is 276.
Input: n = 1
Output: 1
Explanation
$\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}$
Hence the sum of the first 1 natural number is 1.
Input: n = 11
Output: 381876
Explanation
$\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}$
On adding the terms, we get, $\mathrm{1^5 + 2^5 + 3^5 + ... + 11^5 = 381876}$
Hence the sum of the first 11 natural numbers is 381876.
Intuitive Approach
Compute the fifth power of each number, one by one, using an iterative loop.
Create a variable to store the sum after each iteration of the loop.
Display the answer.
Algorithm
Function main()
Initialise n.
Function Call sumOfFifthPower().
Print the sum.
Function sumOfFifthPower(int n)
Initialise sum = 0
for (i from 1 to n)
sum = sum + (pow(i,5)
return sum
Example
The program computes the fifth power of each number and adds it to the existing sum in every iteration by using a for loop which is implemented n times in the function 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
Analysis of Time and Space
Time Complexity: O(n), as there is a single for loop used inside the function sumOfFifthPower().
Space Complexity: O(1), as no extra space is used.
Alternate Approach
Compute the sum of the fifth power of each number using a mathematical formula.
Display the answer.
Formula
$$\mathrm{\displaystyle\sum\limits_{k=1}^n \:k^5=\frac{1}{12}(2n^6+6n^5+5n^4−n^2)}$$
Algorithm
Function main()
Initialise n.
Function Call sumOfFifthPower().
Print the sum.
Function sumOfFifthPower(int n)
Initialise sum = 0
sum = ((2 * pow(n,6)) + (6 * pow(n,5) + (5 * pow(n,4) - (pow(n,2)) / 12
return sum
Example
The program computes the sum by substituting the value of n in a mathematical formula which calculates the summation of the fifth power of the first n natural numbers in the function 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
Analysis of Time and Space
Time Complexity: O(1) as the answer is calculated in a single iteration using a direct formula.
Space Complexity: O(1) as no extra space is needed.
Comparing the Above Approaches
Criteria | Approach 1 | Approach 2 |
---|---|---|
Time Complexity | O(n) | O(1) |
Space Complexity | O(1) | O(1) |
Intuitiveness | More | Less |
Efficiency | Less | More |
Conclusion
This article discusses two approaches to finding the sum of the fifth powers of the first n natural numbers. It also provides us with the concept of the approach, algorithm used, C++ program solution as well as the complexity analysis of each method. It can be observed that while the first approach has a greater time complexity, but it is way more intuitive. On the other hand, the second approach solves the problem efficiently in O(1) time and space using a direct mathematical formula.