-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbreakpoints.py
More file actions
39 lines (29 loc) · 837 Bytes
/
breakpoints.py
File metadata and controls
39 lines (29 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def test_sequence(N):
"""
Compute the infinite sum of 2^{-n} starting from n = 0, truncating
at n = N, returning the value of 2^{-n} and the truncated sum.
Parameters
----------
N : int
Positive integer, giving the number of terms in the sum
Returns
-------
limit : float
The value of 2^{-N}
partial_sum : float
The value of the truncated sum
Notes
-----
The limiting value should be zero, and the value of the sum should
converge to 2.
"""
# Start sum from zero, so give zeroth term
limit = 1.0
partial_sum = 1.0
# At each step, increment sum and change summand
for n in range(1, N+1):
partial_sum += limit
limit /= 2.0
return limit, partial_sum
if __name__ == '__main__':
print(test_sequence(50))