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

  1. Simple Approach: O(n2)
    1. Total number of term in series = n+1
    2. Calculate each term separately, and add them:

    Below is the implementation of the above approach:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ implementation of the approach
      
    #include <bits/stdc++.h>
    using namespace std;
      
    // Function to return sum
    int sum(int k, int n)
    {
      
        int sum = 0;
        for (int i = 0; i <= n; i++) {
            int p = 1;
      
            for (int j = 0; j < n - i; j++) {
                p = p * k;
            }
      
            for (int j = 0; j < i; j++) {
                p = p * (k - 1);
            }
      
            sum = sum + p;
        }
        return sum;
    }
      
    // Driver code
    int main()
    {
        int n = 3;
        int K = 3;
        cout << sum(K, n);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java implementation of the approach
    class GFG {
        // Function to return sum
        static int sum(int k, int n)
        {
      
            int sum = 0;
            for (int i = 0; i <= n; i++) {
                int p = 1;
      
                for (int j = 0; j < n - i; j++) {
                    p = p * k;
                }
      
                for (int j = 0; j < i; j++) {
                    p = p * (k - 1);
                }
      
                sum = sum + p;
            }
            return sum;
        }
      
        // Driver code
        public static void main(String[] args)
        {
            int n = 3;
            int K = 3;
            System.out.println(sum(K, n));
        }
    }
      
    // This code is contributed by Code_Mech

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 implementation of the approach
      
    # Function to return sum
    def Sum(k, n):
      
        Summ = 0
        for i in range(n + 1):
            p = 1
      
            for j in range(n - i):
                p = p * k
      
            for j in range(i):
                p = p * (k - 1)
              
            Summ = Summ + p
          
        return Summ
      
    # Driver code
    n = 3
    K = 3
    print(Sum(K, n))
      
    # This code is contributed by mohit kumar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# implementation of the approach
      
    using System;
      
    class GFG {
        // Function to return sum
        static int sum(int k, int n)
        {
      
            int sum = 0;
            for (int i = 0; i <= n; i++) {
                int p = 1;
      
                for (int j = 0; j < n - i; j++) {
                    p = p * k;
                }
      
                for (int j = 0; j < i; j++) {
                    p = p * (k - 1);
                }
      
                sum = sum + p;
            }
            return sum;
        }
      
        // Driver code
        public static void Main()
        {
            int n = 3;
            int K = 3;
            Console.WriteLine(sum(K, n));
        }
        // This code is contributed by Ryuga
    }

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP implementation of the approach
      
    // Function to return sum
    function sum($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;
    echo sum($K, $n);
      
    // This code is contributed 
    // by Akanksha Rai
    ?>

    chevron_right

    
    

    Output:

    65
    

    Time Complexity: O( n^2 )

  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++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    // Function to return sum
    int sum(int k, int n)
    {
        int sum
            = pow(k, n + 1)
              - pow(k - 1, n + 1);
      
        return sum;
    }
      
    // Driver code
    int main()
    {
        int n = 3;
        int K = 3;
        cout << sum(K, n);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java implementation of above approach
    class GFG
    {
      
    // Function to return sum
    static int sum(int k, int n)
    {
        int sum = (int)(Math.pow(k, n + 1) - 
                        Math.pow(k - 1, n + 1));
      
        return sum;
    }
      
    // Driver code
    public static void main(String args[])
    {
        int n = 3;
        int K = 3;
        System.out.print(sum(K, n));
    }
    }
      
    // This code is contributed
    // by Akanksha Rai

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Function to return sum 
    def sum(k, n):
        sum = (pow(k, n + 1) - 
               pow(k - 1, n + 1)); 
      
        return sum
      
    # Driver code 
    n = 3
    K = 3
    print(sum(K, n)); 
      
    # This code is contributed by mits

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# implementation of above approach
    using System;
      
    class GFG
    {
      
    // Function to return sum
    static int sum(int k, int n)
    {
        int sum = (int)(Math.Pow(k, n + 1) - 
                        Math.Pow(k - 1, n + 1));
      
        return sum;
    }
      
    // Driver code
    public static void Main()
    {
        int n = 3;
        int K = 3;
        Console.Write(sum(K, n));
    }
    }
      
    // This code is contributed
    // by Akanksha Rai

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Function to return sum
    function sum($k, $n)
    {
        $sum = pow($k, $n + 1) - 
               pow($k - 1, $n + 1);
      
        return $sum;
    }
      
    // Driver code
    $n = 3;
    $K = 3;
    echo sum($K, $n);
      
    // This code is contributed
    // by Akanksha Rai

    chevron_right

    
    

    Output:

    65
    

    Time Complexity: O( n )

  3. 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++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    // Recursive C program to compute modular power
    int exponent(int A, int B)
    {
        // Base cases
        if (A == 0)
            return 0;
        if (B == 0)
            return 1;
      
        // If B is even
        long y;
        if (B % 2 == 0) {
            y = exponent(A, B / 2);
            y = (y * y);
        }
      
        // If B is odd
        else {
            y = A;
            y = (y * exponent(A, B - 1));
        }
      
        return y;
    }
      
    // Function to return sum
    int sum(int k, int n)
    {
        int sum = exponent(k, n + 1)
                  - exponent(k - 1, n + 1);
      
        return sum;
    }
      
    // Driver code
    int main()
    {
        int n = 3;
        int K = 3;
        cout << sum(K, n);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.lang.Math;
      
    class GFG
    {
          
    // Recursive C program to compute modular power
    static int exponent(int A, int B)
    {
        // Base cases
        if (A == 0)
            return 0;
        if (B == 0)
            return 1;
      
        // If B is even
        int y;
        if (B % 2 == 0
        {
            y = exponent(A, B / 2);
            y = (y * y);
        }
      
        // If B is odd
        else 
        {
            y = A;
            y = (y * exponent(A, B - 1));
        }
      
        return y;
    }
      
    // Function to return sum
    static int sum(int k, int n)
    {
        int sum = exponent(k, n + 1)
                - exponent(k - 1, n + 1);
      
        return sum;
    }
      
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        int K = 3;
        System.out.println(sum(K, n));
    }
    }
      
    // This code is contributed by Code_Mech.

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Recursive python3 program to compute modular power
      
    def exponent(A, B):
        # Base cases
        if (A == 0):
            return 0;
        if (B == 0):
            return 1;
      
        # If B is even
        if (B % 2 == 0):
            y = exponent(A, B / 2);
            y = (y * y);
      
        # If B is odd
        else:
            y = A;
            y = (y * exponent(A, B - 1));
      
        return y;
      
    # Function to return sum
    def sum(k, n):
        sum = exponent(k, n + 1) - exponent(k - 1, n + 1);
      
        return sum;
      
    # Driver code
    n = 3;
    K = 3;
    print(sum(K, n));
      
    # This code has been contributed by 29AjayKumar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program of above approach
    using System;
      
    class GFG
    {
          
    // Recursive C program to compute modular power
    static int exponent(int A, int B)
    {
        // Base cases
        if (A == 0)
            return 0;
        if (B == 0)
            return 1;
      
        // If B is even
        int y;
        if (B % 2 == 0) 
        {
            y = exponent(A, B / 2);
            y = (y * y);
        }
      
        // If B is odd
        else
        {
            y = A;
            y = (y * exponent(A, B - 1));
        }
      
        return y;
    }
      
    // Function to return sum
    static int sum(int k, int n)
    {
        int sum = exponent(k, n + 1)
                - exponent(k - 1, n + 1);
      
        return sum;
    }
      
    // Driver code
    public static void Main()
    {
        int n = 3;
        int K = 3;
        Console.WriteLine(sum(K, n));
    }
    }
      
    // This code is contributed by Code_Mech.

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // Recursive C program to compute modular power
      
    function exponent($A, $B)
    {
        // Base cases
        if ($A == 0)
            return 0;
        if ($B == 0)
            return 1;
      
        // If B is even
        if ($B % 2 == 0) 
        {
            $y = exponent($A, $B / 2);
            $y = ($y * $y);
        }
      
        // If B is odd
        else
        {
            $y = $A;
            $y = ($y * exponent($A, $B - 1));
        }
      
        return $y;
    }
      
    // Function to return sum
    function sum($k, $n)
    {
        $sum = exponent($k, $n + 1) - 
               exponent($k - 1, $n + 1);
      
        return $sum;
    }
      
    // Driver code
    $n = 3;
    $K = 3;
    echo sum($K, $n);
      
    // This code is contributed by Akanksha Rai
    ?>

    chevron_right

    
    

    Output:

    65
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.