Did you know ... | Search Documentation: |
Pack nan_numerics_prime -- prolog/nan_numerics_prime.pl |
library(nan_numerics_prime)
Module prime
provides predicates to test (positive integer) numbers for
primality, find divisors and factor numbers, generate prime numbers in some
interval, find consecutive prime numbers, and save/load all prime numbers
up to some value to/from a file or stream.
All predicates in module prime
are safe, i.e. validate input arguments
and ensure steadfastness. For maximum performance, user code can directly
call the unsafe public
(not exported) predicates in module prime_lgc
.
Implements a variant of the Miller-Rabin primality test that is
deterministic for numbers up to 3317044064679887385961980
, otherwise
it is probabilistic with the number of iterations fixed at 20
.
For better performance, leverages a prime wheel of level 4
, i.e.
generated by the first 4
consecutive prime numbers, and the memoization
of pairs of consecutive prime numbers.
NOTE: Since the primality test in use is probabilistic in general, this module is not suitable for cryptographic applications.
This library was developed and tested with: SWI-Prolog 7.3.25 - http://www.swi-prolog.org/
Usage example:
?- pack_install(nan_numerics_prime). true. ?- use_module(library(nan_numerics_prime)). true. ?- time(prime_right(1234567891012345678901234567890123456789011111, P)). % 1,205 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips) P = 1234567891012345678901234567890123456789011139. ?- time(prime_lgc:right_(1234567891012345678901234567890123456789011111, P)). % 1,197 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips) P = 1234567891012345678901234567890123456789011139.
The corresponding unsafe predicate is prime_lgc:test_/1
.
The corresponding unsafe predicate is prime_lgc:div_/2
.
The corresponding unsafe predicate is prime_lgc:div_rev_/2
.
Elements of PFs are of the form P^F
with P the prime divisor and
F the corresponding power.
If N is equal to 1
or if N is a prime number, PFs is [N^1]
.
The corresponding unsafe predicate is prime_lgc:fact_/2
.
2
and 3
, and less than or
equal to Sup in the variant with arity 3
. Fails if the prime to the
left of Sup is less than the prime to the right of Inf.
The corresponding unsafe predicates are prime_lgc:gen_/2-3
, and
prime_lgc:gen_p_/2-3
if the bounds are definitely prime.
3
.
Fails if Sup is equal to 1
or if the prime to the left of Sup is less
than the prime to the right of Inf.
The corresponding unsafe predicates are prime_lgc:gen_rev_/2-3
,
and prime_lgc:gen_rev_p_/2-3
if the bounds are definitely prime.
The corresponding unsafe predicates are prime_lgc:next_/2
, and
prime_lgc:next_p_/2
if N is definitely prime.
2
.
The corresponding unsafe predicates are prime_lgc:prev_/2
, and
prime_lgc:prev_p_/2
if N is definitely prime.
The corresponding unsafe predicate is prime_lgc:right_/2
.
1
.
The corresponding unsafe predicate is prime_lgc:left_/2
.
2
that
generate the underlying prime wheel. 2
. Fails if Sup is equal to
1
.
The accepted file format is a comma-separated list of the consecutive
prime numbers starting from 2
and terminated by a period. The file
must not be empty.
Encoding of file is ascii
, type is text
, stream buffer size is
1024
.
NOTE: Clears the memoization table before loading.
2
and less
than or equal to Sup. Fails if Sup is equal to 1
.
The produced file format is a comma-separated list of the consecutive
prime numbers starting from 2
and terminated by a period.
Encoding of file is ascii
, type is text
, buffering is full
,
stream buffer size is 1024
.
2
. Fails if Sup is equal to
1
.
The accepted file format is a comma-separated list of the consecutive
prime numbers starting from 2
and terminated by a period. The file
must not be empty.
Encoding of stream is ascii
, type is text
, buffer size is 1024
.
NOTE: Clears the memoization table before loading.
2
and
less than or equal to Sup. Fails if Sup is equal to 1
.
The produced file format is a comma-separated list of the consecutive
prime numbers starting from 2
and terminated by a period.
Encoding of stream is ascii
, type is text
, buffering is full
,
buffer size is 1024
.
Extends library(error)
with the following types:
prime | Prime number |
pfact | P^F with P prime and F posint |
file | fname or fpipe |
fname | text |
fpipe | atom or string |
stream | Stream identifier |
posint | positive_integer |
arith(Type) | Arithmetic expr. that evaluates to Type |
var(Type) | var or Type |
or(Type1, Type2) | Type1 or Type2 |
The following predicates are exported, but not or incorrectly documented.