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) = 98Input: N = 8, K = 4
Output: 8772
Approach:
- The idea is to find the value of xK using pow() function. (where x is from 1 to N).
- 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^Kint 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 Codeint 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^Kclass 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^Kfrom math import pow# Function to find value of# 1^K + 2^K + 3^K + .. + N^Kdef 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 Codeif __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^Kusing 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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



