Program to find value of 1^k + 2^k + 3^k + … + n^k

Given two positive integers N and K. The task is to evaluate the value of 1K + 2 K + 3K + … + NK.

Examples

Input: N = 3, K = 4
Output: 98
Explanation:
∑(x4) = 14 + 24 + 34, where 1 ≤ x ≤ N
∑(x4) = 1 + 16 + 81
∑(x4) = 98



Input: N = 8, K = 4
Output: 8772

Approach:

  1. The idea is to find the value of xK using pow() function. (where x is from 1 to N).
  2. The required sum is the summation of all values calculated above.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find the value
// 1^K + 2^K + 3^K + .. + N^K
#include <bits/stdc++.h>
using namespace std;
  
// Function to find value of
// 1^K + 2^K + 3^K + .. + N^K
int findSum(int N, int k)
{
    // Initialise sum to 0
    int sum = 0;
    for (int i = 1; i <= N; i++) {
  
        // Find the value of
        // pow(i, 4) and then
        // add it to the sum
        sum += pow(i, k);
    }
  
    // Return the sum
    return sum;
}
  
// Drivers Code
int main()
{
    int N = 8, k = 4;
  
    // Function call to
    // find the sum
    cout << findSum(N, k) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the value
// 1^K + 2^K + 3^K + .. + N^K
class GFG {
      
    // Function to find value of
    // 1^K + 2^K + 3^K + .. + N^K
    static int findSum(int N, int k)
    {
        // Initialise sum to 0
        int sum = 0;
        for (int i = 1; i <= N; i++) {
      
            // Find the value of
            // pow(i, 4) and then
            // add it to the sum
            sum += (int)Math.pow(i, k);
        }
      
        // Return the sum
        return sum;
    }
      
    // Drivers Code
    public static void main (String[] args) 
    {
        int N = 8, k = 4;
      
        // Function call to
        // find the sum
        System.out.println(findSum(N, k));
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find the value
// 1^K + 2^K + 3^K + .. + N^K
  
using System;
  
public class GFG {
      
    // Function to find value of
    // 1^K + 2^K + 3^K + .. + N^K
    static int findSum(int N, int k)
    {
        // Initialise sum to 0
        int sum = 0;
        for (int i = 1; i <= N; i++) {
      
            // Find the value of
            // pow(i, 4) and then
            // add it to the sum
            sum += (int)Math.Pow(i, k);
        }
      
        // Return the sum
        return sum;
    }
      
    // Drivers Code
    public static void Main (string[] args) 
    {
        int N = 8, k = 4;
      
        // Function call to
        // find the sum
        Console.WriteLine(findSum(N, k));
    }
  
}
  
// This code is contributed by AnkitRai01

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find the value
# 1^K + 2^K + 3^K + .. + N^K
from math import pow
  
# Function to find value of
# 1^K + 2^K + 3^K + .. + N^K
def findSum(N, k):
      
    # Initialise sum to 0
    sum = 0
    for i in range(1, N + 1, 1):
          
        # Find the value of
        # pow(i, 4) and then
        # add it to the sum
        sum += pow(i, k)
  
    # Return the sum
    return sum
  
# Drives Code
if __name__ == '__main__':
    N = 8
    k = 4
  
    # Function call to
    # find the sum
    print(int(findSum(N, k)))
  
# This code is contributed by Surendra_Gangwar

chevron_right


Output:

8772

Time Complexity: O(N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




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.