-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework4.py
More file actions
46 lines (40 loc) · 991 Bytes
/
homework4.py
File metadata and controls
46 lines (40 loc) · 991 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
40
41
42
43
44
45
46
from pygments.lexers import math
def alternating_sum_to_ln2(x):
res = 0.0
for i in range(1, x + 1):
res += (-1) ** (i + 1) / i
return res
def rearranged_sum(x):
res = 0.0
pos = 3
count_pos = 1
count_neg = 2
neg = 4
count = 0
while count < x:
for i in range(pos):
if count < x:
res += 1 / count_pos
count_pos += 1
count += 1
else:
break
for i in range(neg):
if count < x:
res -= 1 / count_neg
count_neg += 1
count += 1
else:
break
return res
if __name__ == "__main__":
ln2 = math.log(2)
n = 100000
sum1 = alternating_sum_to_ln2(n)
print("Alternating sum: ")
print ("ln2 = ", ln2)
print ("sum = ", sum1, '\n')
sum2 = rearranged_sum(n)
print("Rearranged sum: ")
print("ln2 = ", ln2)
print("sum = ", sum2)