Compute the ratio of the sum of cube of the first 'n' natural numbers to the sum of square of first 'n' natural numbers.
Code:
input_int=[x for x in range (1,input_int+1)]
python=reduce(lambda x,y:x+y **3/sum(input_int) % x +y**2/sum(input_int),input_int)
x=10
Solution output:
62.96363636363636
Expected output:
7.857142857142857
Your code is very hard to understand and does not even run.
Based on your description of the goal, I can suggest a simpler code to achieve it:
def calc_ratio(n):
sum_3 = sum(x**3 for x in range(1, n+1))
sum_2 = sum(x**2 for x in range(1, n+1))
return sum_3 / sum_2
This would give these results:
>>> calc_ratio(10)
7.857142857142857
>>> calc_ratio(100)
75.3731343283582
>>> calc_ratio(1000)
750.3748125937032
>>> calc_ratio(10000)
7500.374981250938
>>> calc_ratio(100000)
75000.37499812501
from functools import reduce
import ast,sys
input_int = int(sys.stdin.read())
num_list = [x for x in range(1, input_int+1)]
print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list))
Try the below code:
from functools import reduce
import ast,sys
input_int = int(sys.stdin.read())
num_list = [x for x in range(1, input_int+1)]
print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y **2 ,num_list))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.