Friday, September 5, 2025
HomeData Modelling & AIProgram to find value of 1^k + 2^k + 3^k + …...

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




// 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;
}


Java




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


Python 3




# 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


C#




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


Javascript




<script>
    // Javascript Program to find the value
    // 1^K + 2^K + 3^K + .. + N^K
     
    // Function to find value of
    // 1^K + 2^K + 3^K + .. + N^K
    function findSum(N, k)
    {
        // Initialise sum to 0
        let sum = 0;
        for (let i = 1; i <= N; i++) {
       
            // Find the value of
            // pow(i, 4) and then
            // add it to the sum
            sum += Math.pow(i, k);
        }
       
        // Return the sum
        return sum;
    }
     
    let N = 8, k = 4;
       
    // Function call to
    // find the sum
    document.write(findSum(N, k));
 
// This code is contributed by divyesh072019.
</script>


Output: 

8772

 

Time Complexity: O(N * log(k)) (here we iterate for loop from i = 1 to N and we need to calculate pow (i,k) which required log(k) time so overall time complexity will be O(N * log(k)) )
Auxiliary space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS