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

10 Jun, 2021
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.
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++
Java
Python 3
C#
Javascript
// 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 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 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# 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
<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>
8772
 
Time Complexity: O(N) 
SURENDRA_GANGWAR
ankthon
divyesh072019
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Merge two sorted arrays with O(1) extra space
Segment Tree | Set 1 (Sum of given range)
Fizz Buzz Implementation
Check if a number is Palindrome
Count ways to reach the n'th stair
Product of Array except itself
Find Union and Intersection of two unsorted arrays
Median of two sorted arrays of same size