python - 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.
解决方案
有一个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
推荐阅读
- javascript - 仅当另一个元素在类中有一定变化时如何显示一个元素
- c++ - 5 月 Leetcode Speedrun 问题:有序数组中的单个元素
- laravel - 如何仅显示以前未选择的项目的下拉列表
- c++ - C++ 中用于读写的最有效的嵌套数组容器?
- sql - Oracle 查询向上或向下舍入到最近的 15 分钟间隔
- php - 如何使用 laravel 6 通过小字符串加密图像名称
- c# - Webclient 没有下载图片
- linux - 如何获得我的程序使用的最大线程数?
- c - 在 C Char 中从 Pointer char 获取 Number
- c# - Firebase 仍在读取已删除的数据