Sum of kth powers of first n natural numbers
Given two integers n and k, the task is to calculate and print 1k + 2k + 3k + … + nk.
Examples:
Input: n = 5, k = 2
Output: 55
12 + 22 + 32 + 42 + 52 = 1 + 4 + 9 + 16 + 25 = 55Input: n = 10, k = 4
Output: 25333
Approach: Proof for sum of squares of first n natural numbers:
(n+1)3 – n3 = 3 * (n2) + 3 * n + 1
putting n = 1, 2, 3, 4, …, n
23 – 13 = 3 * (12) + 3 * 1 + 1 …equation 1
33 – 23 = 3 * (22) + 3 * 2 + 1 …equation 2
43 – 33 = 3 * (32) + 3 * 3 + 1 …equation 3
……
……
……
(n + 1)3 – n3 = 3 * (n2) + 3 * n + 1 …equation n
Adding all equations:
(n + 1)3 – 13 = 3 * (sum of square terms) + 3 * (sum of n terms) + n
n3 + 3 * n2 + 3 * n = 3 * (sum of square terms) + (3 * n * (n + 1)) / 2 + n
sum of square terms = (n * (n + 1) * (2 * n + 1)) / 6
Similarly, proof for cubes can be shown by taking:
(n+1)4 – n4 = 4 * n3 + 6 * n2 + 4 * n + 1
if we continue this process for n5, n6, n7 … nk
Sum of squares,
(n + 1)3 – 13 = 3C1 * sum(n2) + 3C2 * sum(n) + 3C3
Using sum of squares we can find the sum of cubes,
(n + 1)4 – 14 = 4C1 * sum(n3) + 4C2 * sum(n2) + 4C3 * sum(n) + 4C4
Similarly for kth powers sum,
(n + 1)k – 1k = kC1 * sum(n(k – 1)) + kC2 * sum(n(k – 2)) + … + kC(k – 1) * (sum(n^(k-(k-1)) + kCk * n
where C stands for binomial coefficients
Use modulus function for higher values of n
Below is the implementation of the above approach:
C++
// C++ program to find sum pf k-th powers of // first n natural numbers. #include <bits/stdc++.h> using namespace std; // A global array to store factorials const int MAX_K = 15; long long unsigned int fac[MAX_K]; // Function to calculate the factorials // of all the numbers upto k void factorial(int k) { fac[0] = 1; for (int i = 1; i <= k + 1; i++) { fac[i] = (i * fac[i - 1]); } } // Function to return the binomial coefficient long long unsigned int bin(int a, int b) { // nCr = (n! * (n - r)!) / r! long long unsigned int ans = ((fac[a]) / (fac[a - b] * fac[b])); return ans; } // Function to return the sum of kth powers of // n natural numbers long int sumofn(int n, int k) { int p = 0; long long unsigned int num1, temp, arr[1000]; for (int j = 1; j <= k; j++) { // When j is unity if (j == 1) { num1 = (n * (n + 1)) / 2; // Calculating sum(n^1) of unity powers // of n; storing sum(n^1) for sum(n^2) arr[p++] = num1; // If k = 1 then temp is the result temp = num1; } else { temp = (pow(n + 1, j + 1) - 1 - n); // For finding sum(n^k) removing 1 and // n * kCk from (n + 1)^k for (int s = 1; s < j; s++) { // Removing all kC2 * sum(n^(k - 2)) // + ... + kCk - 1 * (sum(n^(k - (k - 1)) temp = temp - (arr[j - s - 1] * bin(j + 1, s + 1)); } temp = temp / (j + 1); // Storing the result for next sum of // next powers of k arr[p++] = temp; } } temp = arr[p - 1]; return temp; } // Driver code int main() { int n = 5, k = 2; factorial(k); cout << sumofn(n, k) << "\n"; return 0; } |
Java
// Java program to find sum pf k-th powers of // first n natural numbers. import java.io.*; class GFG { // A global array to store factorials static int MAX_K = 15; static int fac[] = new int[MAX_K]; // Function to calculate the factorials // of all the numbers upto k static void factorial(int k) { fac[0] = 1; for (int i = 1; i <= k + 1; i++) { fac[i] = (i * fac[i - 1]); } } // Function to return the binomial coefficient static int bin(int a, int b) { // nCr = (n! * (n - r)!) / r! int ans = ((fac[a]) / (fac[a - b] * fac[b])); return ans; } // Function to return the sum of kth powers of // n natural numbers static int sumofn(int n, int k) { int p = 0; int num1, temp; int arr[] = new int[1000]; for (int j = 1; j <= k; j++) { // When j is unity if (j == 1) { num1 = (n * (n + 1)) / 2; // Calculating sum(n^1) of unity powers // of n; storing sum(n^1) for sum(n^2) arr[p++] = num1; // If k = 1 then temp is the result temp = num1; } else { temp = ((int)Math.pow(n + 1, j + 1) - 1 - n); // For finding sum(n^k) removing 1 and // n * kCk from (n + 1)^k for (int s = 1; s < j; s++) { // Removing all kC2 * sum(n^(k - 2)) // + ... + kCk - 1 * (sum(n^(k - (k - 1)) temp = temp - (arr[j - s - 1] * bin(j + 1, s + 1)); } temp = temp / (j + 1); // Storing the result for next sum of // next powers of k arr[p++] = temp; } } temp = arr[p - 1]; return temp; } // Driver code public static void main (String[] args) { int n = 5, k = 2; factorial(k); System.out.println( sumofn(n, k)); } } // This code is contributed by anuj_67.. |
Python3
# Python3 program to find the sum pf k-th # powers of first n natural numbers # global array to store factorials MAX_K = 15fac = [1 for i in range(MAX_K)] # function to calculate the factorials # of all the numbers upto k def factorial(k): fac[0] = 1 for i in range(1, k + 2): fac[i] = (i * fac[i - 1]) # function to return the binomial coeff def bin(a, b): # nCr=(n!*(n-r)!)/r! ans = fac[a] // (fac[a - b] * fac[b]) return ans # function to return the sum of the kth # powers of n natural numbers def sumofn(n, k): p = 0 num1, temp = 1, 1 arr = [1 for i in range(1000)] for j in range(1, k + 1): # when j is 1 if j == 1: num1 = (n * (n + 1)) // 2 # calculating sum(n^1) of unity powers #of n storing sum(n^1) for sum(n^2) arr[p] = num1 p += 1 # if k==1 then temp is the result else: temp = pow(n + 1, j + 1) - 1 - n # for finding sum(n^k) removing 1 and # n*KCk from (n+1)^k for s in range(1, j): # Removing all kC2 * sum(n^(k - 2)) # + ... + kCk - 1 * (sum(n^(k - (k - 1)) temp = temp - (arr[j - s - 1] * bin(j + 1, s + 1)) temp = temp // (j + 1) # storing the result for next sum # of next powers of k arr[p] = temp p += 1 temp = arr[p - 1] return temp # Driver code n, k = 5, 2factorial(k) print(sumofn(n, k)) # This code is contributed by Mohit kumar 29 |
C#
// C# program to find sum pf k-th powers of // first n natural numbers. using System; class GFG { // A global array to store factorials static int MAX_K = 15; static int []fac = new int[MAX_K]; // Function to calculate the factorials // of all the numbers upto k static void factorial(int k) { fac[0] = 1; for (int i = 1; i <= k + 1; i++) { fac[i] = (i * fac[i - 1]); } } // Function to return the binomial coefficient static int bin(int a, int b) { // nCr = (n! * (n - r)!) / r! int ans = ((fac[a]) / (fac[a - b] * fac[b])); return ans; } // Function to return the sum of kth powers of // n natural numbers static int sumofn(int n, int k) { int p = 0; int num1, temp; int []arr = new int[1000]; for (int j = 1; j <= k; j++) { // When j is unity if (j == 1) { num1 = (n * (n + 1)) / 2; // Calculating sum(n^1) of unity powers // of n; storing sum(n^1) for sum(n^2) arr[p++] = num1; // If k = 1 then temp is the result temp = num1; } else { temp = ((int)Math.Pow(n + 1, j + 1) - 1 - n); // For finding sum(n^k) removing 1 and // n * kCk from (n + 1)^k for (int s = 1; s < j; s++) { // Removing all kC2 * sum(n^(k - 2)) // + ... + kCk - 1 * (sum(n^(k - (k - 1)) temp = temp - (arr[j - s - 1] * bin(j + 1, s + 1)); } temp = temp / (j + 1); // Storing the result for next sum of // next powers of k arr[p++] = temp; } } temp = arr[p - 1]; return temp; } // Driver code public static void Main () { int n = 5, k = 2; factorial(k); Console.WriteLine(sumofn(n, k)); } // This code is contributed by Ryuga } |
PHP
55
Recommended Posts:
- Sum of fifth powers of the first n natural numbers
- Sum of fourth powers of first n odd natural numbers
- Sum of fourth powers of the first n natural numbers
- Sum of sum of first n natural numbers
- Sum of cubes of even and odd natural numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Average of first n even natural numbers
- Sum of all odd natural numbers in range L and R
- Sum of all natural numbers in range L to R
- Sum of cubes of first n odd natural numbers
- Sum of squares of first n natural numbers
- Sum of squares of first n natural numbers
- Sum of the natural numbers (up to N) whose modulo with K yield R
- Program to find sum of first n natural numbers
- Find sum of N-th group of Natural Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



