The Computer Language
24.11 Benchmarks Game

pidigits Julia #4 program

source code

# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

# contributed by Brad Chamberlain
# based on the Chapel #2 version and Python (#1) version

using Printf

function pidigits(numDigits::Int)
    numer,accum,denom,tmp1,tmp2 = map(BigInt,(1,0,1,0,0))

    i = k = 1
    ns = d = d2 = 0

    while i <= numDigits
        (accum,numer,denom) = nextTerm(k,accum,numer,denom)
        k += 1
        if numer <= accum
            (d,tmp1,tmp2) = extractDigit(3,numer,accum,denom,tmp1,tmp2)
            (d2,tmp1,tmp2) = extractDigit(4,numer,accum,denom,tmp1,tmp2)
            if d == d2
                ns = ns * 10 + d
                if i % 10 == 0
                    @printf("%d\t:%d\n",ns,i)
                    ns = 0
                end
 
                (accum,numer) = eliminateDigit(d,accum,denom,numer)
                i += 1
            end
        end
    end

    # Pad out any trailing digits for the final line
    if numDigits % 10 != 0
        @printf("%d",ns)
        for i in 1:(10-numDigits%10)
            @printf(" ")
        end
        @printf("\t:%d\n",n)
     end
end

function nextTerm(k::Int64, accum::BigInt, numer::BigInt, denom::BigInt)
    k2 = 2 * k + 1

    accum += numer*2
    accum *= k2
    denom *= k2
    numer *= k
    (accum,numer,denom)
end

function extractDigit(nth::Int64, numer::BigInt, accum::BigInt, denom::BigInt, tmp1::BigInt, tmp2::BigInt)
    tmp1 = numer * nth
    tmp2 = tmp1 + accum
    (tmp1,_) = divrem(tmp2,denom)

    (convert(Int64,tmp1),tmp1,tmp2)
end

function eliminateDigit(d::Int64, accum::BigInt, denom::BigInt, numer::BigInt)
    accum -= denom * d
    accum *= 10
    numer *= 10
    (accum,numer)
end

n = parse(Int,ARGS[1])
pidigits(n)
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
julia version 1.11.1


 Fri, 20 Dec 2024 17:11:12 GMT

MAKE:
printenv JULIA_NUM_THREADS
4

0.08s to complete and log all make actions

COMMAND LINE:
 /opt/src/julia-1.11.1/bin/julia -O3 --cpu-target=ivybridge --math-mode=ieee  -- pidigits.julia-4.julia 2000

UNEXPECTED OUTPUT 

6c6
< 582097494	:60
---
> 0582097494	:60
28c28
< 454326648	:280
---
> 0454326648	:280
34,35c34,35
< 962829254	:340
< 917153643	:350
---
> 0962829254	:340
> 0917153643	:350
37c37
< 11330530	:370
---
> 0011330530	:370
53c53
< 860213949	:530
---
> 0860213949	:530
73c73
< 864034418	:730
---
> 0864034418	:730
76c76
< 518707211	:760
---
> 0518707211	:760
102c102
< 106548586	:1020
---
> 0106548586	:1020
120c120
< 984882401	:1200
---
> 0984882401	:1200
123c123
< 471018194	:1230
---
> 0471018194	:1230
137c137
< 355876402	:1370
---
> 0355876402	:1370
145c145
< 286182974	:1450
---
> 0286182974	:1450
161c161
< 816470600	:1610
---
> 0816470600	:1610
184c184
< 950680064	:1840
---
> 0950680064	:1840

PROGRAM OUTPUT:
3141592653	:10
5897932384	:20
6264338327	:30
9502884197	:40
1693993751	:50
582097494	:60
4592307816	:70
4062862089	:80
9862803482	:90
5342117067	:100
9821480865	:110
1328230664	:120
7093844609	:130
5505822317	:140
2535940812	:150
8481117450	:160
2841027019	:170
3852110555	:180
9644622948	:190
9549303819	:200
6442881097	:210
5665933446	:220
1284756482	:230
3378678316	:240
5271201909	:250
1456485669	:260
2346034861	:270
454326648	:280
2133936072	:290
6024914127	:300
3724587006	:310
6063155881	:320
7488152092	:330
962829254	:340
917153643	:350
6789259036	:360
11330530	:370
5488204665	:380
2138414695	:390
1941511609	:400
4330572703	:410
6575959195	:420
3092186117	:430
3819326117	:440
9310511854	:450
8074462379	:460
9627495673	:470
5188575272	:480
4891227938	:490
1830119491	:500
2983367336	:510
2440656643	:520
860213949	:530
4639522473	:540
7190702179	:550
8609437027	:560
7053921717	:570
6293176752	:580
3846748184	:590
6766940513	:600
2000568127	:610
1452635608	:620
2778577134	:630
2757789609	:640
1736371787	:650
2146844090	:660
1224953430	:670
1465495853	:680
7105079227	:690
9689258923	:700
5420199561	:710
1212902196	:720
864034418	:730
1598136297	:740
7477130996	:750
518707211	:760
3499999983	:770
7297804995	:780
1059731732	:790
8160963185	:800
9502445945	:810
5346908302	:820
6425223082	:830
5334468503	:840
5261931188	:850
1710100031	:860
3783875288	:870
6587533208	:880
3814206171	:890
7766914730	:900
3598253490	:910
4287554687	:920
3115956286	:930
3882353787	:940
5937519577	:950
8185778053	:960
2171226806	:970
6130019278	:980
7661119590	:990
9216420198	:1000
9380952572	:1010
106548586	:1020
3278865936	:1030
1533818279	:1040
6823030195	:1050
2035301852	:1060
9689957736	:1070
2259941389	:1080
1249721775	:1090
2834791315	:1100
1557485724	:1110
2454150695	:1120
9508295331	:1130
1686172785	:1140
5889075098	:1150
3817546374	:1160
6493931925	:1170
5060400927	:1180
7016711390	:1190
984882401	:1200
2858361603	:1210
5637076601	:1220
471018194	:1230
2955596198	:1240
9467678374	:1250
4944825537	:1260
9774726847	:1270
1040475346	:1280
4620804668	:1290
4259069491	:1300
2933136770	:1310
2898915210	:1320
4752162056	:1330
9660240580	:1340
3815019351	:1350
1253382430	:1360
355876402	:1370
4749647326	:1380
3914199272	:1390
6042699227	:1400
9678235478	:1410
1636009341	:1420
7216412199	:1430
2458631503	:1440
286182974	:1450
5557067498	:1460
3850549458	:1470
8586926995	:1480
6909272107	:1490
9750930295	:1500
5321165344	:1510
9872027559	:1520
6023648066	:1530
5499119881	:1540
8347977535	:1550
6636980742	:1560
6542527862	:1570
5518184175	:1580
7467289097	:1590
7772793800	:1600
816470600	:1610
1614524919	:1620
2173217214	:1630
7723501414	:1640
4197356854	:1650
8161361157	:1660
3525521334	:1670
7574184946	:1680
8438523323	:1690
9073941433	:1700
3454776241	:1710
6862518983	:1720
5694855620	:1730
9921922218	:1740
4272550254	:1750
2568876717	:1760
9049460165	:1770
3466804988	:1780
6272327917	:1790
8608578438	:1800
3827967976	:1810
6814541009	:1820
5388378636	:1830
950680064	:1840
2251252051	:1850
1739298489	:1860
6084128488	:1870
6269456042	:1880
4196528502	:1890
2210661186	:1900
3067442786	:1910
2203919494	:1920
5047123713	:1930
7869609563	:1940
6437191728	:1950
7467764657	:1960
5739624138	:1970
9086583264	:1980
5995813390	:1990
4780275900	:2000