The Computer Language
25.02 Benchmarks Game

pidigits Java -Xint #4 program

source code

/* The Computer Language Benchmarks Game
 * https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
 *
 * Contributed by Michael Ganss
 * derived from PHP version that was
 * contributed by Oleksii Prudkyi
 * port from pidigits.lua-5.lua (Mike Pall, Wim Couwenberg)
 * modified by Craig Russell
 *
 * Original C version by Mr Ledrug
 *
 * ported to Java by Piotr Tarsa
 */

import jextract_gmp.gmp_h;

import java.lang.foreign.Arena;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;

public class pidigits {
    private static final Arena GLOBAL_ARENA = Arena.global();

    // Manually written as jextract didn't extract it.
    // Names and types taken from gmp.h
    private static final MemoryLayout mpz_t = MemoryLayout.structLayout(
            gmp_h.C_INT.withName("_mp_alloc"),
            gmp_h.C_INT.withName("_mp_size"),
            gmp_h.C_POINTER.withName("_mp_d")
    ).withName("__mpz_struct");

    @SuppressWarnings("SameParameterValue")
    private static MemorySegment alloc(MemoryLayout typeDescription) {
        return GLOBAL_ARENA.allocate(typeDescription);
    }

    public static void main(String[] args) {
        MemorySegment n1 = alloc(mpz_t),
                n2 = alloc(mpz_t),
                d = alloc(mpz_t),
                u = alloc(mpz_t),
                v = alloc(mpz_t),
                w = alloc(mpz_t);
        int k = 1, k2, i = 0;
        var n = Integer.parseInt(args[0]);

        gmp_h.__gmpz_init(u);
        gmp_h.__gmpz_init(v);

        gmp_h.__gmpz_init_set_si(w, 0);
        gmp_h.__gmpz_init_set_si(n1, 4);
        gmp_h.__gmpz_init_set_si(n2, 3);
        gmp_h.__gmpz_init_set_si(d, 1);

        var outBuf = new StringBuilder();

        for (; ; ) {
            gmp_h.__gmpz_tdiv_q(u, n1, d);
            gmp_h.__gmpz_tdiv_q(v, n2, d);

            if (gmp_h.__gmpz_cmp(u, v) == 0) {
                outBuf.append(gmp_h.__gmpz_get_si(u));
                i++;
                if (i % 10 == 0)
                    outBuf.append("\t:").append(i).append("\n");
                if (i == n)
                    break;

                // extract
                gmp_h.__gmpz_mul_si(u, u, -10);
                gmp_h.__gmpz_mul(u, d, u);
                gmp_h.__gmpz_mul_si(n1, n1, 10);
                gmp_h.__gmpz_add(n1, n1, u);
                gmp_h.__gmpz_mul_si(n2, n2, 10);
                gmp_h.__gmpz_add(n2, n2, u);
            } else {
                // produce
                k2 = k * 2;
                gmp_h.__gmpz_mul_si(u, n1, k2 - 1);
                gmp_h.__gmpz_add(v, n2, n2);
                gmp_h.__gmpz_mul_si(w, n1, k - 1);
                gmp_h.__gmpz_add(n1, u, v);
                gmp_h.__gmpz_mul_si(u, n2, k + 2);
                gmp_h.__gmpz_add(n2, w, u);
                gmp_h.__gmpz_mul_si(d, d, k2 + 1);
                k++;
            }
        }

        if (i % 10 != 0)
            outBuf.append(" ".repeat(10 - n % 10)).append("\t:").append(n).append("\n");

        System.out.print(outBuf);
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
java 23 2024-09-17
Java HotSpot(TM) 64-Bit Server VM
(build 23+37-2369, 
interpreted mode, sharing)


 Mon, 17 Feb 2025 04:39:23 GMT

MAKE:
mv pidigits.javaxint-4.javaxint pidigits.java
/opt/src/jdk-23/bin/javac -d . -cp . --source-path Include/java pidigits.java

4.48 seconds to complete and log all make actions

COMMAND LINE:
 /opt/src/jdk-23/bin/java -Xint  -cp . pidigits 2000

UNEXPECTED OUTPUT 

1,5d0
< WARNING: A restricted method in java.lang.foreign.SymbolLookup has been called
< WARNING: java.lang.foreign.SymbolLookup::libraryLookup has been called by jextract_gmp.gmp_h in an unnamed module
< WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
< WARNING: Restricted methods will be blocked in a future release unless native access is enabled
< 

PROGRAM OUTPUT:
WARNING: A restricted method in java.lang.foreign.SymbolLookup has been called
WARNING: java.lang.foreign.SymbolLookup::libraryLookup has been called by jextract_gmp.gmp_h in an unnamed module
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled

3141592653	:10
5897932384	:20
6264338327	:30
9502884197	:40
1693993751	:50
0582097494	: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
0454326648	:280
2133936072	:290
6024914127	:300
3724587006	:310
6063155881	:320
7488152092	:330
0962829254	:340
0917153643	:350
6789259036	:360
0011330530	: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
0860213949	: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
0864034418	:730
1598136297	:740
7477130996	:750
0518707211	: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
0106548586	: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
0984882401	:1200
2858361603	:1210
5637076601	:1220
0471018194	: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
0355876402	:1370
4749647326	:1380
3914199272	:1390
6042699227	:1400
9678235478	:1410
1636009341	:1420
7216412199	:1430
2458631503	:1440
0286182974	: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
0816470600	: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
0950680064	: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