Related Articles
Program to find value of 1^k + 2^k + 3^k + … + n^k
• Difficulty Level : Basic
• Last Updated : 07 Apr, 2020

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

## 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`

## 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`
Output:
```8772
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up