Q:

How to use faulhaber sequence in python?

I am trying to code a function. The function accepts two parameters, k and n. It should return the sum of the k-th powers of the numbers from 1 to n.
For example, sumPowerN(1, 3) should return 6
The answer for the above example is 6 because 1^1 + 2^1 + 3^1 = 6
This is what I've done so far;
def sumPowerN(k,n):

    result = 0

    for n in range(1, n+1, n):

        result = result + (1 ** k) + (2 ** k) + (n ** k)

    return result

def main():

    print("Program to calculate sum of k-th powers of numbers from 1 to n")

    kVal, nVal = input("Please enter the k-th value and the n-th value (k,n): ")

    answer = sumPowerN(kVal, nVal)

    print("The value of the sum is:", answer ,".")

main()

Please help. I am really stuck. And please do point out what I'm doing wrong because I am still new to Python.

A:

def sumPowerN(k,n):

    result = 0

    for n in range(1, n+1):

        result = result + (n ** k)

    return result

def main():

    print("Program to calculate sum of k-th powers of numbers from 1 to n")

    kVal, nVal = input("Please enter the k-th value and the n-th value (k,n): ")

    answer = sumPowerN(kVal, nVal)

    print("The value of the sum is:", answer ,".")

main()

resulted in:
$ python sumPowerN_Cg.py 
Program to calculate sum of k-th powers of numbers from 1 to n
Please enter the k-th value and the n-th value (k,n): 1,3
('The value of the sum is:', 6, '.')

