Skip to content

Instantly share code, notes, and snippets.

@mlliarm
Last active December 22, 2021 10:22
Show Gist options
  • Save mlliarm/97b4c45c3b44beeaa2cc1d2d9217a35b to your computer and use it in GitHub Desktop.
Save mlliarm/97b4c45c3b44beeaa2cc1d2d9217a35b to your computer and use it in GitHub Desktop.
rat 2 dec with J

Rational to decimal with J

What

  • Dividing huge integers in J.
  • In Dyalog APL it's possible too, but the results are not that good as J's.

The integers

p = 169621521599715008276219171512587640647841095442456069002320644182266868459617619753889122898655162183404136973947180752187359905902753260425513050548229467432513862416424894942454713985465278932529328431801828096801920887765994271135011232692744581328925613143089416369332765923820914844366872416498286310709782023849335976275636317740764835591903828756560907312090101454770517747287108619058818385005663195977504011488807928750255014263206030308478008789858365581938685005925857190843815093499376975642422198241136503493946530984537459852215831260865709362658168293949557761906738505201661019132792725659665127352812617496756168506523926285934972259335392327959749209119904008499118043974118384243889708663618255952472658431205999918317575607657740236649567997214307671993864889895393839141039506162002967032097850104329360630620037199839999142283857127423583660420412640713389114512448981659186654920369410109397250843678564086072047059266513900405454076350312323975218753628513160246104286440957540497492228327530693076123214774002565781816122426929452997664845839935152576597214412838642199080522538526785632514871374048614455166761773227562170331518842728241937247564343923818495125376084747425403562295453185186235744629977833817398516530400483769490658446407168075399793934693849926911293460509164677
q = 53992207234726675278961941928450302452822047623718564385150323747724701281112300190300106635808547972512609385998185753170427113716200847706128220369404709717036269053644161045979852353945911645945115429379410105679831222022124449171708573935148545356552619360430097618490856349218462434207739425221688438345427657734945388360754851355876703517025241721040065222250907032187978825511851598073604902050405347475910190484449194196262402616451203465800884163477391039657283777500697293141096800015886292794390305130398878990902018844921870933047995171901635986345004445034969192966427479779117696471307624934148805180535413821861248354813884290062923803439746267128219005135975738755802559525508507324875485007341328779475078894735722877157533192681548668962657606446758705361666748320254297731413043617929690439214061599103967718073490431270227525555253221083788818740709945739088997644645878720664836566515847158951628965993677079199266744574662134053159976336619213638568983033487260628088204640814188076260382314091548602410696939680031879020944602729449902120056631834098794391928608779141524686795239077451960200846097922780695588801746583663247036517735360081178649014537721782789452025926453581219061848116302188155364930920220240513762484943105388217395949984758122066857243133095444913227856394518646

The person that found these two integers by running this python code (now forked), claimed over Reddit that if you divide them (p/q) they'll give you a rational approximation of Pi.

In J a rational of two integers p and q is denoted as prq. That is, the character r separates the nominator p from the denominator q.

Code

Both solutions return the decimal expansion of pi with 5 floating points, i.e. 3.14159. This is what we wanted to confirm, as we got those two long integers from a person in r/math that shared his code that calculates rediculously LARGE integers of which the quotient approximates Pi.

First solution

First solution thanks to bobTerryo from the r/apljk Discord server.

x: inv 169621521599715008276219171512587640647841095442456069002320644182266868459617619753889122898655162183404136973947180752187359905902753260425513050548229467432513862416424894942454713985465278932529328431801828096801920887765994271135011232692744581328925613143089416369332765923820914844366872416498286310709782023849335976275636317740764835591903828756560907312090101454770517747287108619058818385005663195977504011488807928750255014263206030308478008789858365581938685005925857190843815093499376975642422198241136503493946530984537459852215831260865709362658168293949557761906738505201661019132792725659665127352812617496756168506523926285934972259335392327959749209119904008499118043974118384243889708663618255952472658431205999918317575607657740236649567997214307671993864889895393839141039506162002967032097850104329360630620037199839999142283857127423583660420412640713389114512448981659186654920369410109397250843678564086072047059266513900405454076350312323975218753628513160246104286440957540497492228327530693076123214774002565781816122426929452997664845839935152576597214412838642199080522538526785632514871374048614455166761773227562170331518842728241937247564343923818495125376084747425403562295453185186235744629977833817398516530400483769490658446407168075399793934693849926911293460509164677r53992207234726675278961941928450302452822047623718564385150323747724701281112300190300106635808547972512609385998185753170427113716200847706128220369404709717036269053644161045979852353945911645945115429379410105679831222022124449171708573935148545356552619360430097618490856349218462434207739425221688438345427657734945388360754851355876703517025241721040065222250907032187978825511851598073604902050405347475910190484449194196262402616451203465800884163477391039657283777500697293141096800015886292794390305130398878990902018844921870933047995171901635986345004445034969192966427479779117696471307624934148805180535413821861248354813884290062923803439746267128219005135975738755802559525508507324875485007341328779475078894735722877157533192681548668962657606446758705361666748320254297731413043617929690439214061599103967718073490431270227525555253221083788818740709945739088997644645878720664836566515847158951628965993677079199266744574662134053159976336619213638568983033487260628088204640814188076260382314091548602410696939680031879020944602729449902120056631834098794391928608779141524686795239077451960200846097922780695588801746583663247036517735360081178649014537721782789452025926453581219061848116302188155364930920220240513762484943105388217395949984758122066857243133095444913227856394518646

Result:

3.14159

Second solution

Second solution from: https://www.jsoftware.com/help/learning/19.htm.

float=: x: ^: _1
float 169621521599715008276219171512587640647841095442456069002320644182266868459617619753889122898655162183404136973947180752187359905902753260425513050548229467432513862416424894942454713985465278932529328431801828096801920887765994271135011232692744581328925613143089416369332765923820914844366872416498286310709782023849335976275636317740764835591903828756560907312090101454770517747287108619058818385005663195977504011488807928750255014263206030308478008789858365581938685005925857190843815093499376975642422198241136503493946530984537459852215831260865709362658168293949557761906738505201661019132792725659665127352812617496756168506523926285934972259335392327959749209119904008499118043974118384243889708663618255952472658431205999918317575607657740236649567997214307671993864889895393839141039506162002967032097850104329360630620037199839999142283857127423583660420412640713389114512448981659186654920369410109397250843678564086072047059266513900405454076350312323975218753628513160246104286440957540497492228327530693076123214774002565781816122426929452997664845839935152576597214412838642199080522538526785632514871374048614455166761773227562170331518842728241937247564343923818495125376084747425403562295453185186235744629977833817398516530400483769490658446407168075399793934693849926911293460509164677r53992207234726675278961941928450302452822047623718564385150323747724701281112300190300106635808547972512609385998185753170427113716200847706128220369404709717036269053644161045979852353945911645945115429379410105679831222022124449171708573935148545356552619360430097618490856349218462434207739425221688438345427657734945388360754851355876703517025241721040065222250907032187978825511851598073604902050405347475910190484449194196262402616451203465800884163477391039657283777500697293141096800015886292794390305130398878990902018844921870933047995171901635986345004445034969192966427479779117696471307624934148805180535413821861248354813884290062923803439746267128219005135975738755802559525508507324875485007341328779475078894735722877157533192681548668962657606446758705361666748320254297731413043617929690439214061599103967718073490431270227525555253221083788818740709945739088997644645878720664836566515847158951628965993677079199266744574662134053159976336619213638568983033487260628088204640814188076260382314091548602410696939680031879020944602729449902120056631834098794391928608779141524686795239077451960200846097922780695588801746583663247036517735360081178649014537721782789452025926453581219061848116302188155364930920220240513762484943105388217395949984758122066857243133095444913227856394518646

Result:

3.14159

See also

See also more about the motivation of this problem and background at the PARI/GP solution gist.

PARI/GP is a much more powerfull language for numerics as you'll see, since it'll calculate the value of Pi from the above division in 38 decimal places (!), and as fast as we did it in J. Amazing.

Conclusion: PARI/GP is the King in rapid high precision numerics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment