Sum of Powers
EasyPractice library · +10 XPrecursionmathiteration
Given two non-negative integers N and K, calculate the sum of the first N positive integers, each raised to the power of K. That is, compute 1^K + 2^K + 3^K + ... + N^K. You must use recursion to solve this problem.
Input
A single line containing two space-separated integers N and K.
Output
A single integer representing the sum 1^K + 2^K + ... + N^K.
Constraints
0 <= N <= 20, 0 <= K <= 10
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
3 2
Output
14
Calculates 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14.
Example 2
Input
4 1
Output
10
Calculates 1^1 + 2^1 + 3^1 + 4^1 = 1 + 2 + 3 + 4 = 10.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
3 2
Example 2 input
4 1