Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

D example with gmp-d

Links:

gmp-d is a high-level wrapper for the GNU MP (GMP) library. It's mostly compatible with std.bigint.BigInt. Integers (GMP's mpz_t) are wrapped in gmp.z.MpZ.

Since we use a 3rd-party package, it's better to use the DUB package manager. Create a dub project and then add gmp-d as a dependency:

dub add gmp-d
(1) Using gmp-d
#!/usr/bin/env rdmd

import gmp.z;

import std.stdio;

alias Z = MpZ;

void main()
{
    auto n = Z(2) ^^ 31 - 1;

    writeln(n.toString()); // 2147483647

    // 0: definitely composite; 1: probably prime; 2: definitely prime
    writeln(n.isProbablyPrime(10)); // 2, i.e. definitely prime

    writeln(n.isDefinitelyPrime(10)); // true
}

Note:

  • In the example, the argument 10 is the number of Miller-Rabin tests to perform. A higher value increases the certainty of the result for a composite number.
  • DUB packages are downloaded here: ~/.dub/packages
  • Under the hood, gmp-d uses the C library gmp. So you must have gmp installed. Under Manjaro, I could install it with yay -S gmp.

Compile and run:

dub run

Shared library dependencies:

$ ldd ./main
        linux-vdso.so.1 (0x00007fc375608000)
        libgmp.so.10 => /usr/lib/libgmp.so.10 (0x00007fc37541b000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007fc375200000)
        /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007fc37560a000)
        libm.so.6 => /usr/lib/libm.so.6 (0x00007fc3750f2000)
        libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007fc3750c5000)

libgmp.so.10 is the C library that the gmp-d D binding wraps around. The others are the essential system libraries under Linux.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 September 01, 12:24