首页 > 解决方案 > Writing a function to compute a quantity in python based on a math problem

问题描述

Write a function to compute the quantity

F(n) = n^2 Σ i=1 (i^3)

Read the problem as n squared over Sigma, with i = 1 under the sigma and I cubed at the end of the function.

I am not sure how to approach this idea. I tried setting up a function but I do not know how to use a function in Python to compute the problem we were given.

As mentioned above, I am sorry, but I do not know how to approach this problem.

I suppose the expected output here would be some quantity but because I haven't been able to make much progress, I have no clue what to expect exactly. To give more background, I understand how functions work but do not know how to approach this type of problem. Any help/guidance in writing this code would be greatly appreciated.

标签: pythonpython-3.x

解决方案


有一个list comprehension

def my_sigma(n_start: int, n_end: int) -> int:
    return sum([i**3 for i in range(n_start, (n_end**2) + 1)])

# usage
print(my_sigma(1, 3))
>>> 2025

推荐阅读