Skip to content

JulStrat/primesieve-pas

Repository files navigation

Build status release license

primesieve-pas

Pascal bindings for PrimeSieve library.

Repository structure -

  • primesieve.pas - Pascal API unit.
  • docs - API documentation in HTML, LaTex and PDF format. Documentation generated with PasDoc and TeXstudio.
  • examples - Pascal port of original C examples from PrimeSieve, countlets - counts prime tuplets, printlets - prints prime tuplets, totient - prints Euler's totient for range.
  • include - C API v7.7 header files.
  • libprimesieve - v7.7 64bit Windows dynamic libraries.

Requirements

Install primesieve library.

  • Debian, Ubuntu, Raspbian - sudo apt install libprimesieve-dev.
  • MacOS - brew install primesieve.
  • Windows - you can use prebuilt libraries from the folder libprimesieve.
  • Or build library from source.

Linking against libprimesieve v6.x is also possible - build your project with symbol USE_ABI6 defined (Free Pascal compiler command line switch -dUSE_ABI6). ABI changes details - https://github.com/kimwalisch/primesieve/releases/tag/v7.0.

Usage example

program store_primes_in_array;
{$IF Defined(FPC)}
{$MODE Delphi}
{$ENDIF}

{$APPTYPE CONSOLE}
{$POINTERMATH ON}

uses primesieve;

type
  PInt32 = ^Int32;

var
  start, stop, n: UInt64;
  i, size: NativeUInt;
  primes: PInt32;

begin
  start := 0;
  stop := 1000;

  (*  store the primes below 1000  *)
  primes := primesieve_generate_primes(start, stop, size, INT32_PRIMES);

  for i := 0 to size-1 do
    WriteLn(primes[i]);

  primesieve_free(primes);
  n := 1000;

  (*  store the first 1000 primes  *)
  primes := primesieve_generate_n_primes(n, start, INT32_PRIMES);

  for i := 0 to n-1 do
    WriteLn(primes[i]);

  primesieve_free(primes);
end.

Building example

Free Pascal compiler -

$ fpc -B -O2 -CX -XX examples/store_primes_in_array.pas

Embarcadero Delphi compiler -

$ dcc64 -B -$O+ -NSSystem examples/store_primes_in_array.pas
...
$ dcc64 -B -$O+ -NSSystem examples/printlets.pas
$ dcc64 -B -$O+ -NSSystem examples/countlets.pas

Run -

$ time examples/printlets.exe 6 0 1000000
(7, 11, 13, 17, 19, 23)
(97, 101, 103, 107, 109, 113)
(16057, 16061, 16063, 16067, 16069, 16073)
(19417, 19421, 19423, 19427, 19429, 19433)
(43777, 43781, 43783, 43787, 43789, 43793)

real    0m0.038s
user    0m0.000s
sys     0m0.031s

$ time examples/countlets.exe 6 0 1000000000
317

real    0m0.184s
user    0m0.000s
sys     0m0.031s

Links

General information about prime numbers

Prime sieve