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 = 55
Input: 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 of k-th powers of // first n natural numbers.#include <bits/stdc++.h>using namespace std;// A global array to store factorialsconst int MAX_K = 15;long long unsigned int fac[MAX_K];// Function to calculate the factorials// of all the numbers upto kvoid 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 coefficientlong 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 numberslong 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 codeint main(){ int n = 5, k = 2; factorial(k); cout << sumofn(n, k) << "\n"; return 0;} |
Java
// Java program to find sum of k-th powers of // first n natural numbers.import java.io.*;class GFG {// A global array to store factorialsstatic int MAX_K = 15;static int fac[] = new int[MAX_K];// Function to calculate the factorials// of all the numbers upto kstatic 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 coefficientstatic 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 numbersstatic 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 of 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 kdef factorial(k): fac[0] = 1 for i in range(1, k + 2): fac[i] = (i * fac[i - 1])# function to return the binomial coeffdef 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 numbersdef 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 coden, k = 5, 2factorial(k)print(sumofn(n, k))# This code is contributed by Mohit kumar 29 |
C#
// C# program to find sum of 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
<?php// PHP program to find sum of k-th powers // of first n natural numbers.// A global array to store factorial$MAX_K = 15;$fac[$MAX_K] = array();// Function to calculate the factorials// of all the numbers upto kfunction factorial($k){ global $fac; $fac[0] = 1; for ($i = 1; $i <= $k + 1; $i++) { $fac[$i] = ($i * $fac[$i - 1]); }}// Function to return the binomial // coefficientfunction bin($a, $b){ global $MAX_K; global $fac; // nCr = (n! * (n - r)!) / r! $ans = (($fac[$a]) / ($fac[$a - $b] * $fac[$b])); return $ans;}// Function to return the sum of kth // powers of n natural numbersfunction sumofn($n, $k){ $p = 0; $num1; $temp; $arr[1000] = array(); for ($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 ($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$n = 5;$k = 2;factorial($k);echo sumofn($n, $k), "\n";// This code is contributed by Sachin?> |
Javascript
<script>// Javascript program to find sum of k-th powers of // first n natural numbers. // A global array to store factorials var MAX_K = 15; var fac = Array(MAX_K).fill(0); // Function to calculate the factorials // of all the numbers upto k function factorial(k) { fac[0] = 1; for (i = 1; i <= k + 1; i++) { fac[i] = (i * fac[i - 1]); } } // Function to return the binomial coefficient function bin(a , b) { // nCr = (n! * (n - r)!) / r! var ans = ((fac[a]) / (fac[a - b] * fac[b])); return ans; } // Function to return the sum of kth powers of // n natural numbers function sumofn(n , k) { var p = 0; var num1, temp; var arr = Array(1000).fill(0); for (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 = (parseInt( Math.pow(n + 1, j + 1) - 1 - n)); // For finding sum(n^k) removing 1 and // n * kCk from (n + 1)^k for (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 var n = 5, k = 2; factorial(k); document.write(sumofn(n, k));// This code contributed by Rajput-Ji</script> |
55
Complexity Analysis:
Time Complexity: O(k*(log(k)+k)).
In the worst case as we can see in sumofn() function we have a log component and a loop nested so time complexity would be O(k*(log(k)+k)) in the worst-case.
Space Complexity: We have declared a global array of size 15 and an array of size 1000 is declared inside sumofn() function, so space complexity is O(p) where p=(15+1000).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



