- Trending Categories
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
Sum of Fourth Powers of first N natural numbers
The fourth power of a number x is x raised to the power 4 or x4. Natural numbers are all positive integers excluding zero. Thus, the sum of the fourth powers of the first N natural numbers is −
$\mathrm{Sum = 1^4 + 2^4 + 3^4 + 4^4 + … + N^4}$
This article describes some approaches for finding the sum using minimum time and space complexity.
Problem Statement
Given the number N, find the sum $\mathrm{1^4 + 2^4 + 3^4 + 4^4 + … + N^4}$.
Example 1
Input: 3
Output: 98
Explanation
$\mathrm{Sum = 1^4 + 2^4 + 3^4 = 1 + 16 + 81 = 98}$
Example 2
Input: 5
Output: 979
Explanation
$\mathrm{Sum = 1^4 + 2^4 + 3^4 + 4^4 + 5^4 = 1 + 16 + 81 + 256 + 625 = 979}$
Solution 1: Brute Force Approach
The brute force approach for solving the problem will be to simply calculate the fourth powers of all numbers from 1 to N and then add them.
Pseudocode
procedure fourthSum (n) sum = 0 for i = 1 to n: sum = sum + i^4 end for end procedure
Example
In the following program, we’ll calculate the value of x raised to power 4 from x ranging from 1 to n and then add them.
#include<bits/stdc++.h> using namespace std; // Function to find the sum of fourth powers of first n natural numbers long long fourthSum(long long n){ // initializing the sum variable long long sum = 0; // adding the fourth powers of all numbers from 1 to n to sum for (int i = 1 ; i <= n ; i++) { // calculating i raised to the power 4 // pow() function returns double thus converting it to int sum += int(pow(i,4)); } return sum; } int main(){ long long N = 7; cout << "Sum of fourth powers of first " << N << " natural numbers = "; cout << fourthSum(N); }
Output
Sum of fourth powers of first 7 natural numbers = 4676
Time Complexity − O(n) as the pow() function is executed for n times with time complexity of O(4) i.e. constant each. Thus n*O(4) will be O(n).
Space Complexity − O(1) as no extra space is used.
Solution 2: Faulhaber’s Formula
Faulhaber’s Formula gives the general formula for the power sum of the first n positive integers.
$$\mathrm{\displaystyle\sum\limits_{k=1}^n k^p=1^p+2^p+3^p+...+n^p}$$
Deriving the formula for$\mathrm{\sum_{k=1}^nk^4}$:
We know that, $\mathrm{\sum_{k=1}^n k=\frac{n(n+1)}{2},\sum_{k=1}^nk^2=\frac{n(n+1)(2n+1)}{6},\sum_{k=1}^nk^3=\frac{n(n+1)^2}{4}}$
Now, using binomial expansion
$\mathrm{(𝑛 + 1)^5 − 𝑛^5 = 5𝑛^4 + 10𝑛^3 + 10𝑛^2 + 5n + 1….(1)}$
Similarly,
$\mathrm{n^5 −(n − 1)^5 = 5(n − 1)^4 + 10(n − 1)\ 3 + 10(n − 1)^2 + 5(n − 1) + 1 …..(2)}$
Computing till we will have n equations
$\mathrm{2^5 − 1^5 = 5 \cdot 1^4 + 10 \cdot 1^3 + 10 \cdot 1^2 + 5 \cdot 1 + 1 ….(n)}$
Adding both LHS and RHS of equations 1 to n −
$$\mathrm{(n+1)^5−1\cdot\displaystyle\sum\limits_{k=1}^n k^4+10\cdot\displaystyle\sum\limits_{k=1}^n k^3+10\cdot\displaystyle\sum\limits_{k=1}^n k^2+5\cdot\displaystyle\sum\limits_{k=1}^n k+n}$$
Replacing the values of $\mathrm{\sum_{k=1}^nk,\sum_{k=1}^nk^2,\:and\:\sum_{k=1}^nk^3,}$ in the above equation, we can find $\mathrm{\sum_{k=1}^nk^4}$
$$\mathrm{\displaystyle\sum\limits_{k=1}^n k^4=\frac{(n+1)^5−1−10\cdot\sum^n_{k=1}k^3−10\cdot\sum^n_{k=1}k^2−5\cdot\sum^n_{k=1}k−n}{5}}$$
Thus,
$$\mathrm{\displaystyle\sum\limits_{k=1}^n k^4=\frac{n^5}{5}+\frac{n^4}{2}+\frac{n^3}{3}−\frac{n}{30}=\frac{6\cdot n^5+15\cdot n^4+10\cdot n^3−n}{30}}$$
We’ll be using the above formula to calculate the sum of the fourth powers of the first N natural numbers.
Example 1
Input: 3
Output: 98
Explanation
n5 = 243, n4 = 81, n3 = 27, n = 3. Thus, sum = ((6*243) + (15*81) + (10*27) - 3)/30 = 98
Example 2
Input: 9
Output: 15333
Explanation
n5 = 59049, n4 = 6561, n3 = 729, n = 9. Thus, sum = ((6*59049) + (15*6561) + (10*729) - 9)/30 = 15333
Pseudocode −
procedure fourthSum (n) sum = 0 fifth power = n5 fourth power = n4 third power = n3 sum = ((6 * fifth power) + (15 * fourth power) + (10 * third power) - n)/30 end procedure
Example
In the following program, we’ll calculate the sum using Faulhaber’s Formula that is derived above.
#include<bits/stdc++.h> using namespace std; // Function to find the sum of fourth powers of first n natural numbers long long fourthSum(long long n){ // initializing the sum variable long long sum = 0; // calculating n raised to the power 5 // pow() returns double thus changing it to int long long fifthPower = int(pow(n,5)); // calculating n raised to the power 4 long long fourthPower = int(pow(n,4)); // calculating n raised to the power 3 long long thirdPower = int(pow(n,3)); sum = ((6 * fifthPower) + (15 * fourthPower) + (10 * thirdPower) - n)/30; return sum; } int main(){ long long N = 11; cout << "Sum of fourth powers of first " << N << " natural numbers = "; cout << fourthSum(N); }
Output
Sum of fourth powers of first 11 natural numbers = 39974
Time Complexity − O(1) as we are using the direct formula for calculating the sum.
Space Complexity − O(1) as no extra space is used.
Conclusion
In conclusion, in order to find the sum of the fourth powers of the first N natural numbers, we can follow two approaches. The first is to simply calculate the powers and add them. But this approach takes a linear time complexity. Another way to solve it using constant time is using Faulhaber’s formula.