这是打印pi的简短代码。它位于一个名为“pi_print.jl”的文件中,其中包含使用bigint计算pi位数的实际算法。
function main()
firsttime_flag = true
counter = 2000
(k, a, b, a1, b1) = (BigInt(2), BigInt(4), BigInt(1), BigInt(12), BigInt(4))
while counter > 0
(p, q, k) = (k*k, BigInt(2)*k+BigInt(1), k+BigInt(1))
(a, b, a1, b1) = (a1, b1, p*a+q*a1, p*b+q*b1)
(d,d1) = ( div(a,b),div(a1,b1) )
while d == d1 && counter > 0
if firsttime_flag
firsttime_flag = false
println("Pi to 2,000 digits to the right of decimal point is:")
write(stdout,string(d))
println(".")
else
write(stdout,string(d))
counter = counter - 1
end
(a,a1) = ( BigInt(10) * (a % b), BigInt(10) * (a1 % b1) )
(d,d1) = ( div(a,b),div(a1,b1) )
end
end
end
main()
这是输出(我截短了它)
$ julia pi_print.jl
Pi to 2,000 digits to the right of decimal point is:
3.
1415926535897932384626433832795028841971693993751058209749445923078164
0628620899862141592653589793238462643383279502884197169399375105820974
...
使用RePL
$ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.3 (2018-12-18)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> include("pi_print.jl")
Pi to 2,000 digits to the right of decimal point is:
3.
1415926535897932384626433832795028841971693993751058209749445923078164062862
0899862803482534211706798214808651328230664709384460955058223172535940812848
1117450284102701938521105559644622948954930381964428810975665933446128475648
2337867831652712019091456485669234603486104543266482133936072602491412737245
8700660631558817488152092096282925409171536436789259036001133053054882046652
1384146951941511609433057270365759591953092186117381932611793105118548074462
3799627495673518857527248912279381830119491298336733624406566430860213949463
9522473719070217986094370277053921717629317675238467481846766940513200056812
7145263560827785771342757789609173637178721468440901224953430146549585371050
7922796892589235420199561121290219608640344181598136297747713099605187072113
4999999837297804995105973173281609631859502445945534690830264252230825334468
5035261931188171010003137838752886587533208381420617177669147303598253490428
7554687311595628638823537875937519577818577805321712268066130019278766111959
0921642019893809525720106548586327886593615338182796823030195203530185296899
5773622599413891249721775283479131515574857242454150695950829533116861727855
8890750983817546374649393192550604009277016711390098488240128583616035637076
6010471018194295559619894676783744944825537977472684710404753464620804668425
9069491293313677028989152104752162056966024058038150193511253382430035587640
2474964732639141992726042699227967823547816360093417216412199245863150302861
8297455570674983850549458858692699569092721079750930295532116534498720275596
0236480665499119881834797753566369807426542527862551818417574672890977772793
8000816470600161452491921732172147723501414419735685481613611573525521334757
4184946843852332390739414333454776241686251898356948556209921922218427255025
4256887671790494601653466804988627232791786085784383827967976681454100953883
7863609506800642251252051173929848960841284886269456042419652850222106611863
0674427862203919494504712371378696095636437191728746776465757396241389086583
264599581339047802759009