前天是3月14日,π日,想起以前写下的一个计算π值的Python代码,分享一下。The day before yesterday was March 14, π day, share a python code written before.
Python代码如下:
python source code as follows:
import sys
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while 1==1:
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d = a // b
d1 = a1 // b1
while d == d1:
sys.stdout.write(str(d))
sys.stdout.flush()
a, a1 = 10*(a%b), 10*(a1%b1)
d, d1 = a//b, a1//b1
运行后会在屏幕上直接打印出π值。代码短小精悍,因为python自身支持大数运算。
python2和python3下都可以运行。
run it will print out the π value on the screen.code is short because python supports Big Number.
It can run on Python2 and python3
输出结果是:
output result is:
314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692...
Congratulations @onlyone! You received a personal award!
Click here to view your Board
Do not miss the last post from @steemitboard:
Vote for @Steemitboard as a witness and get one more award and increased upvotes!
Congratulations @onlyone! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!