Sum of element's inverses of a list


#1

Everything is in the title : in other words how to pass from L[1,3,4 ] to result [ 1+1/3 + 1/4]?


#2

lets say the list is:

[2, 3, 4, 5, 6]

the result should then be:

[2 + 2 / 3 + 2 / 4 + 2 / 5 + 2 / 6]

?

i just need to understand the math, then i can help with the coding solution


#3

According to your list , the result should be :
[ 1 /2 + 1 / 3 + 1/4 + 1/5 + 1/6 ]
I take the reciprocal of each element in the list and add them together


#4

shouldn’t L[1, 3, 4] result in [ 1 / 1 + 1 / 3 + 1 / 4]?

i mean 1 / 1 is obviously 1, but it makes easier to code and see the pattern.

The solution is then really simple, declare a variable, and name it total:

total = 0

then use a for loop to loop over the list, then add 1 / current_element to total.

where current_element is the loop iterator containing the elements from the list


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.