Jensen's Device: Difference between revisions
Content added Content deleted
Line 114: | Line 114: | ||
i = Ref() |
i = Ref() |
||
def sum(ref_i, lo, hi, term): |
def sum(ref_i, lo, hi, term): |
||
# term is passed by-name, and so is i |
# term is passed by-name, and so is i |
||
temp = 0; |
temp = 0; |
||
for i in range(lo,hi+1): |
for i in range(lo,hi+1): |
||
ref_i.value = i |
ref_i.value = i |
||
temp += term() |
temp += term() |
||
return |
return temp |
||
# note the correspondence between the mathematical notation and the call to sum |
# note the correspondence between the mathematical notation and the call to sum |
||
print (sum (i, 1, 100, lambda: 1.0/i.value))</python> |
print (sum (i, 1, 100, lambda: 1.0/i.value))</python> |
||
Output: 5.18737751764 |
Output: 5.18737751764 |