-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (84 loc) · 5.09 KB
/
Copy pathmain.py
File metadata and controls
102 lines (84 loc) · 5.09 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sys
PI_1000 = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821\
48086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233\
78678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036\
00113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279\
38183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263\
56082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477\
13099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865\
8753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019"
PI = (
"3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148\
08651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344\
61284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209\
62829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105\
11854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370\
27705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301\
46549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059\
73173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914\
73035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572\
01065485863278865936153381827968230301952035301852968995773622599413891249721775283479131515574857242454150695950\
82953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559\
61989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935\
11253382430035587640247496473263914199272604269922796782354781636009341721641219924586315030286182974555706749838\
50549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181\
84175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233\
23907394143334547762416862518983569485562099219222184272550254256887671790494601653466804988627232791786085784383\
82796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203\
91949450471237137869609563643719172874677646575739624138908658326459958133904780275900994657640789512694683983525\
95709825822620522489407726719478268482601476990902640136394437455305068203496252451749399651431429809190659250937\
22169646151570985838741059788595977297549893016175392846813826868386894277415599185592524595395943104997252468084\
59872736446958486538367362226260991246080512438843904512441365497627807977156914359977001296160894416948685558484\
06353422072225828488648158456028506016842739452267467678895252138522549954666727823986456596116354886230577456498\
03559363456817432411251507606947945109659609402522887971089314566913686722874894056010150330861792868092087476091\
78249385890097149096759852613655497818931297848216829989487226588048575640142704775551323796414515237462343645428\
58444795265867821051141354735739"
)
def calc_pi_bbp_c(start_idx: int, end_idx: int) -> str:
from decimal import Decimal, getcontext
getcontext().prec = 1000
pi = Decimal(0)
comm = Decimal(0)
deno = Decimal(1)
for _ in range(start_idx, end_idx):
a = 4 / (comm + 1)
b = 2 / (comm + 4)
c = 1 / (comm + 5)
d = 1 / (comm + 6)
pi += (a - b - c - d) / deno
comm += 8
deno *= 16
return str(pi)
def calc_pi_bbp_py(start_idx: int, end_idx: int) -> str:
from _pydecimal import Decimal, getcontext
getcontext().prec = 1000
pi = Decimal(0)
comm = Decimal(0)
deno = Decimal(1)
for _ in range(start_idx, end_idx):
a = 4 / (comm + 1)
b = 2 / (comm + 4)
c = 1 / (comm + 5)
d = 1 / (comm + 6)
pi += (a - b - c - d) / deno
comm += 8
deno *= 16
return str(pi)
def cmp_pi(pi: str):
to_digit = min(len(pi), len(PI))
for i in range(to_digit):
if PI[i] != pi[i]:
print(f"Pi is different at digit {i}")
return
print("All digits are matched!")
if len(sys.argv) < 3:
print("Wrong argument number.")
print("Usage: python main.py <TYPE> <CNT>")
exit(1)
ty = sys.argv[1]
cnt = sys.argv[2]
if ty.upper() == "C":
pi = calc_pi_bbp_c(0, int(cnt))
else:
pi = calc_pi_bbp_py(0, int(cnt))
cmp_pi(pi)