Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ……. (K-1)n
Given a value K and n, the task is to find the sum of the below series:
Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ……. (K-1)n
Examples:
Input: n = 3, K = 3 Output: 65 Explanation: (3*3*3) + (3*3*2) + (3*2*2) + (2*2*2) = 27 + 18 + 12 + 8 = 65 Input: n = 4, k = 2 Output: 31 Explanation: (2*2*2*2) + (2*2*2*1)+ (2*2*1*1) + (2*1*1*1) + (1*1*1*1) = 16 + 8 + 4 + 2 + 1 = 31
- Simple Approach: O(n2)
- Total number of term in series = n+1
- Calculate each term seperately, and add them:
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>usingnamespacestd;// Function to return sumintsum(intk,intn){intsum = 0;for(inti = 0; i <= n; i++) {intp = 1;for(intj = 0; j < n - i; j++) {p = p * k;}for(intj = 0; j < i; j++) {p = p * (k - 1);}sum = sum + p;}returnsum;}// Driver codeintmain(){intn = 3;intK = 3;cout << sum(K, n);}chevron_rightfilter_noneJava
// Java implementation of the approachclassGFG {// Function to return sumstaticintsum(intk,intn){intsum =0;for(inti =0; i <= n; i++) {intp =1;for(intj =0; j < n - i; j++) {p = p * k;}for(intj =0; j < i; j++) {p = p * (k -1);}sum = sum + p;}returnsum;}// Driver codepublicstaticvoidmain(String[] args){intn =3;intK =3;System.out.println(sum(K, n));}}// This code is contributed by Code_Mechchevron_rightfilter_nonePython3
# Python3 implementation of the approach# Function to return sumdefSum(k, n):Summ=0foriinrange(n+1):p=1forjinrange(n-i):p=p*kforjinrange(i):p=p*(k-1)Summ=Summ+preturnSumm# Driver coden=3K=3print(Sum(K, n))# This code is contributed by mohit kumarchevron_rightfilter_noneC#
// C# implementation of the approachusingSystem;classGFG {// Function to return sumstaticintsum(intk,intn){intsum = 0;for(inti = 0; i <= n; i++) {intp = 1;for(intj = 0; j < n - i; j++) {p = p * k;}for(intj = 0; j < i; j++) {p = p * (k - 1);}sum = sum + p;}returnsum;}// Driver codepublicstaticvoidMain(){intn = 3;intK = 3;Console.WriteLine(sum(K, n));}// This code is contributed by Ryuga}chevron_rightfilter_nonePHP
<?php// PHP implementation of the approach// Function to return sumfunctionsum($k,$n){$sum= 0;for($i= 0;$i<=$n;$i++){$p= 1;for($j= 0;$j<$n-$i;$j++){$p=$p*$k;}for($j= 0;$j<$i;$j++){$p=$p* ($k- 1);}$sum=$sum+$p;}return$sum;}// Driver code$n= 3;$K= 3;echosum($K,$n);// This code is contributed// by Akanksha Rai?>chevron_rightfilter_noneOutput:65
Time Complexity: O( n^2 )
- Second Approach: O(n)
It is observed that, given series is Geometric Progression, with common ratio = (K – 1)/K
So, above expression can be simplified as:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>usingnamespacestd;// Function to return sumintsum(intk,intn){intsum=pow(k, n + 1)-pow(k - 1, n + 1);returnsum;}// Driver codeintmain(){intn = 3;intK = 3;cout << sum(K, n);}chevron_rightfilter_noneJava
// Java implementation of above approachclassGFG{// Function to return sumstaticintsum(intk,intn){intsum = (int)(Math.pow(k, n +1) -Math.pow(k -1, n +1));returnsum;}// Driver codepublicstaticvoidmain(String args[]){intn =3;intK =3;System.out.print(sum(K, n));}}// This code is contributed// by Akanksha Raichevron_rightfilter_nonePython3
# Function to return sumdefsum(k, n):sum=(pow(k, n+1)-pow(k-1, n+1));returnsum;# Driver coden=3;K=3;print(sum(K, n));# This code is contributed by mitschevron_rightfilter_noneC#
// C# implementation of above approachusingSystem;classGFG{// Function to return sumstaticintsum(intk,intn){intsum = (int)(Math.Pow(k, n + 1) -Math.Pow(k - 1, n + 1));returnsum;}// Driver codepublicstaticvoidMain(){intn = 3;intK = 3;Console.Write(sum(K, n));}}// This code is contributed// by Akanksha Raichevron_rightfilter_nonePHP
<?php// Function to return sumfunctionsum($k,$n){$sum= pow($k,$n+ 1) -pow($k- 1,$n+ 1);return$sum;}// Driver code$n= 3;$K= 3;echosum($K,$n);// This code is contributed// by Akanksha Raichevron_rightfilter_noneOutput:65
Time Complexity: O( n )
- Third Approach (Efficient): O(log n)
Note: Time complexity can further be reduced to O(log(n)), by calculating power in log(n) complexity.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>usingnamespacestd;// Recursive C program to compute modular powerintexponent(intA,intB){// Base casesif(A == 0)return0;if(B == 0)return1;// If B is evenlongy;if(B % 2 == 0) {y = exponent(A, B / 2);y = (y * y);}// If B is oddelse{y = A;y = (y * exponent(A, B - 1));}returny;}// Function to return sumintsum(intk,intn){intsum = exponent(k, n + 1)- exponent(k - 1, n + 1);returnsum;}// Driver codeintmain(){intn = 3;intK = 3;cout << sum(K, n);}chevron_rightfilter_noneJava
importjava.lang.Math;classGFG{// Recursive C program to compute modular powerstaticintexponent(intA,intB){// Base casesif(A ==0)return0;if(B ==0)return1;// If B is eveninty;if(B %2==0){y = exponent(A, B /2);y = (y * y);}// If B is oddelse{y = A;y = (y * exponent(A, B -1));}returny;}// Function to return sumstaticintsum(intk,intn){intsum = exponent(k, n +1)- exponent(k -1, n +1);returnsum;}// Driver codepublicstaticvoidmain(String[] args){intn =3;intK =3;System.out.println(sum(K, n));}}// This code is contributed by Code_Mech.chevron_rightfilter_nonePython3
# Recursive python3 program to compute modular powerdefexponent(A, B):# Base casesif(A==0):return0;if(B==0):return1;# If B is evenif(B%2==0):y=exponent(A, B/2);y=(y*y);# If B is oddelse:y=A;y=(y*exponent(A, B-1));returny;# Function to return sumdefsum(k, n):sum=exponent(k, n+1)-exponent(k-1, n+1);returnsum;# Driver coden=3;K=3;print(sum(K, n));# This code has been contributed by 29AjayKumarchevron_rightfilter_noneC#
// C# program of above approachusingSystem;classGFG{// Recursive C program to compute modular powerstaticintexponent(intA,intB){// Base casesif(A == 0)return0;if(B == 0)return1;// If B is eveninty;if(B % 2 == 0){y = exponent(A, B / 2);y = (y * y);}// If B is oddelse{y = A;y = (y * exponent(A, B - 1));}returny;}// Function to return sumstaticintsum(intk,intn){intsum = exponent(k, n + 1)- exponent(k - 1, n + 1);returnsum;}// Driver codepublicstaticvoidMain(){intn = 3;intK = 3;Console.WriteLine(sum(K, n));}}// This code is contributed by Code_Mech.chevron_rightfilter_nonePHP
<?php// Recursive C program to compute modular powerfunctionexponent($A,$B){// Base casesif($A== 0)return0;if($B== 0)return1;// If B is evenif($B% 2 == 0){$y= exponent($A,$B/ 2);$y= ($y*$y);}// If B is oddelse{$y=$A;$y= ($y* exponent($A,$B- 1));}return$y;}// Function to return sumfunctionsum($k,$n){$sum= exponent($k,$n+ 1) -exponent($k- 1,$n+ 1);return$sum;}// Driver code$n= 3;$K= 3;echosum($K,$n);// This code is contributed by Akanksha Rai?>chevron_rightfilter_noneOutput:65
Recommended Posts:
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))
- Sum of the series 1.2.3 + 2.3.4 + ... + n(n+1)(n+2)
- Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n
- Sum of Series (n^2-1^2) + 2(n^2-2^2) +....n(n^2-n^2)
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ...... + (1+2+3+4+...+n)
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .
- Sum of series 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)!
- Sum of series 1*1! + 2*2! + ……..+ n*n!
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Sum of the series 2^0 + 2^1 + 2^2 +.....+ 2^n
- Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + …… + (2+4+6+8+….+2n)
- Sum of the series 3, 20, 63, 144, ......
- Sum of the first N terms of the series 2,10, 30, 68,....
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.



