Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
18
Task/Jensens-Device/Python/jensens-device-1.py
Normal file
18
Task/Jensens-Device/Python/jensens-device-1.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
class Ref(object):
|
||||
def __init__(self, value=None):
|
||||
self.value = value
|
||||
|
||||
def harmonic_sum(i, lo, hi, term):
|
||||
# term is passed by-name, and so is i
|
||||
temp = 0
|
||||
i.value = lo
|
||||
while i.value <= hi: # Python "for" loop creates a distinct which
|
||||
temp += term() # would not be shared with the passed "i"
|
||||
i.value += 1 # Here the actual passed "i" is incremented.
|
||||
return temp
|
||||
|
||||
i = Ref()
|
||||
|
||||
# note the correspondence between the mathematical notation and the
|
||||
# call to sum it's almost as good as sum(1/i for i in range(1,101))
|
||||
print harmonic_sum(i, 1, 100, lambda: 1.0/i.value)
|
||||
5
Task/Jensens-Device/Python/jensens-device-2.py
Normal file
5
Task/Jensens-Device/Python/jensens-device-2.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def harmonic_sum(i, lo, hi, term):
|
||||
return sum(term() for i[0] in range(lo, hi + 1))
|
||||
|
||||
i = [0]
|
||||
print(harmonic_sum(i, 1, 100, lambda: 1.0 / i[0]))
|
||||
5
Task/Jensens-Device/Python/jensens-device-3.py
Normal file
5
Task/Jensens-Device/Python/jensens-device-3.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def harmonic_sum(i, lo, hi, term):
|
||||
return sum(eval(term) for i[0] in range(lo, hi + 1))
|
||||
|
||||
i = [0]
|
||||
print(harmonic_sum(i, 1, 100, "1.0 / i[0]"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue