Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Julia 1.0+;中;,打印小数点后pi的前2000位数字_Julia_Arbitrary Precision_Pii - Fatal编程技术网

在Julia 1.0+;中;,打印小数点后pi的前2000位数字

在Julia 1.0+;中;,打印小数点后pi的前2000位数字,julia,arbitrary-precision,pii,Julia,Arbitrary Precision,Pii,出于教学目的,您将如何编写一个“julian”Julia 1.0+代码来打印超过小数点的pi的前2000位数字?Julia REPL中的结果应该与此匹配 预期产出为: julia> include("solution.jl") Pi to 2,000 digits to right of decimal point is: 3. 14159265358979323846264338327950288419716939937510 582097494459230781640628620899

出于教学目的,您将如何编写一个“julian”Julia 1.0+代码来打印超过小数点的
pi
的前2000位数字?Julia REPL中的结果应该与此匹配

预期产出为:

julia> include("solution.jl")
Pi to 2,000 digits to right of decimal point is:
3.
14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679
82148086513282306647093844609550582231725359408128
48111745028410270193852110555964462294895493038196
44288109756659334461284756482337867831652712019091
45648566923460348610454326648213393607260249141273
72458700660631558817488152092096282925409171536436
78925903600113305305488204665213841469519415116094
33057270365759591953092186117381932611793105118548
07446237996274956735188575272489122793818301194912
98336733624406566430860213949463952247371907021798
60943702770539217176293176752384674818467669405132
00056812714526356082778577134275778960917363717872
14684409012249534301465495853710507922796892589235
42019956112129021960864034418159813629774771309960
51870721134999999837297804995105973173281609631859
50244594553469083026425223082533446850352619311881
71010003137838752886587533208381420617177669147303
59825349042875546873115956286388235378759375195778
18577805321712268066130019278766111959092164201989
38095257201065485863278865936153381827968230301952
03530185296899577362259941389124972177528347913151
55748572424541506959508295331168617278558890750983
81754637464939319255060400927701671139009848824012
85836160356370766010471018194295559619894676783744
94482553797747268471040475346462080466842590694912
93313677028989152104752162056966024058038150193511
25338243003558764024749647326391419927260426992279
67823547816360093417216412199245863150302861829745
55706749838505494588586926995690927210797509302955
32116534498720275596023648066549911988183479775356
63698074265425278625518184175746728909777727938000
81647060016145249192173217214772350141441973568548
16136115735255213347574184946843852332390739414333
45477624168625189835694855620992192221842725502542
56887671790494601653466804988627232791786085784383
82796797668145410095388378636095068006422512520511
73929848960841284886269456042419652850222106611863
06744278622039194945047123713786960956364371917287
46776465757396241389086583264599581339047802759009

对于Julia的一个新来者来说,用很少的代码解决这个问题的一个令人惊讶的关键是,Julia中的
pi
常量并不像许多其他语言那样硬编码到一定的位数。在Julia中,
pi
常量根据当前操作的基本精度“更改”其精度,例如,在本例中,Julia的当前
BigFloat
精度

因此,如果我们将
BigFloat
精度设置为2001个十进制数字,并使用
pi
执行
BigFloat
结果操作,我们应该得到所需的答案

据我所知,执行此操作的最简单方法是将
pi
更改为
BigFloat
类型。这将导致结果类型为
BigFloat
,如下代码所示

julia> VERSION
v"1.1.0"

julia> typeof(BigFloat(pi))  # big"1" * pi also works
BigFloat
使用此方法可以解决原始问题,如下代码所示。请注意,在
setprecision
输入计算中可能不需要使用2002和2001十进制数字,但我至少使用了一个额外的数字作为任何可能的舍入的“保护”数字

setprecision(Int(ceil(log2(10) * 2002))) do
    pi_str = string(BigFloat(pi))[1:3+1999]
    digits_str = pi_str[3:3+1999]
    println("Pi to 2,000 digits to right of decimal point is:")
    println(pi_str[1:2])
    for start in 1:50:1951
        println(digits_str[start:start+49])
    end
end
我发现Julia用这么少的代码就能解决这个问题,真是不可思议!我也尝试过小数点后10000位,效果很好

以下是输出:

julia> include("solution.jl")
Pi to 2,000 digits to right of decimal point is:
3.
14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679
82148086513282306647093844609550582231725359408128
48111745028410270193852110555964462294895493038196
44288109756659334461284756482337867831652712019091
45648566923460348610454326648213393607260249141273
72458700660631558817488152092096282925409171536436
78925903600113305305488204665213841469519415116094
33057270365759591953092186117381932611793105118548
07446237996274956735188575272489122793818301194912
98336733624406566430860213949463952247371907021798
60943702770539217176293176752384674818467669405132
00056812714526356082778577134275778960917363717872
14684409012249534301465495853710507922796892589235
42019956112129021960864034418159813629774771309960
51870721134999999837297804995105973173281609631859
50244594553469083026425223082533446850352619311881
71010003137838752886587533208381420617177669147303
59825349042875546873115956286388235378759375195778
18577805321712268066130019278766111959092164201989
38095257201065485863278865936153381827968230301952
03530185296899577362259941389124972177528347913151
55748572424541506959508295331168617278558890750983
81754637464939319255060400927701671139009848824012
85836160356370766010471018194295559619894676783744
94482553797747268471040475346462080466842590694912
93313677028989152104752162056966024058038150193511
25338243003558764024749647326391419927260426992279
67823547816360093417216412199245863150302861829745
55706749838505494588586926995690927210797509302955
32116534498720275596023648066549911988183479775356
63698074265425278625518184175746728909777727938000
81647060016145249192173217214772350141441973568548
16136115735255213347574184946843852332390739414333
45477624168625189835694855620992192221842725502542
56887671790494601653466804988627232791786085784383
82796797668145410095388378636095068006422512520511
73929848960841284886269456042419652850222106611863
06744278622039194945047123713786960956364371917287
46776465757396241389086583264599581339047802759009

下面是打印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()
以下是输出(我已截断)

使用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

朱莉娅:是的,那确实是一篇非常好的文章!谢谢你指出这一点。我不知道这是如何教你计算圆周率的。我更愿意说,这是Julia如何处理一些无理常数而不舍入的简单演示。@hckr您是对的。这里的教学不是关于π的计算,而是关于Julia中的非理性类型如何工作,与许多其他语言相比。@hckr根据您的评论,我从问题的标题和第一句中删除了“计算”一词。我认为这更准确地解释了这种方法显示如何“打印”pi,而不是“计算”pi。感谢您的澄清。感谢Steven分享pi的算法。它基于哪一个公式?
$ 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