API reference
Every public method of every class, grouped by purpose.
All classes belong to the namespace PHPMath\NumberTheory. The exceptions belong to PHPMath\NumberTheory\Exception. Numbers are BigInteger objects from PHPMath\BigInteger\BigInteger unless a parameter is typed int. Classes in the PHPMath\NumberTheory\Internal namespace, such as GmpBridge, are internal. They are not listed here and are not covered by the stability promise, so do not use them.
Divisibility#
Divisors, greatest common divisor, least common multiple and the extended Euclidean algorithm. A final class of static methods.
divides()#
public static function divides(BigInteger $divisor, BigInteger $value): bool
Checks whether the divisor divides the value without a remainder. Zero only divides zero.
- Parameters:
$divisorthe divisor,$valuethe value to divide - Returns:
bool
isCoprime()#
public static function isCoprime(BigInteger $a, BigInteger $b): bool
Checks whether two numbers share no prime factor, which means their gcd is 1.
- Parameters:
$athe first number,$bthe second number - Returns:
bool
gcd()#
public static function gcd(BigInteger $a, BigInteger $b): BigInteger
Returns the non-negative greatest common divisor.
- Parameters:
$athe first number,$bthe second number - Returns:
BigInteger
lcm()#
public static function lcm(BigInteger $a, BigInteger $b): BigInteger
Returns the non-negative least common multiple.
- Parameters:
$athe first number,$bthe second number - Returns:
BigInteger
extendedGcd()#
public static function extendedGcd(BigInteger $a, BigInteger $b): ExtendedGcd
Runs the extended Euclidean algorithm.
- Parameters:
$athe first number,$bthe second number - Returns:
ExtendedGcdwith the gcd and coefficientsxandyfor whicha * x + b * y = gcd
ExtendedGcd#
The immutable result of the extended Euclidean algorithm. It is a final readonly class with the public properties gcd, x and y, all of type BigInteger. For the inputs a and b it holds that a * x + b * y = gcd.
__construct()#
public function __construct(BigInteger $gcd, BigInteger $x, BigInteger $y)
Creates a result. The values are not checked against any inputs.
- Parameters:
$gcdthe non-negative gcd,$xthe coefficient of the first input,$ythe coefficient of the second input
satisfiesBezout()#
public function satisfiesBezout(BigInteger $a, BigInteger $b): bool
Checks that a * x + b * y equals the gcd.
- Parameters:
$athe first input of the algorithm,$bthe second input of the algorithm - Returns:
bool
ModularArithmetic#
Modular exponentiation and modular inverses. A final class of static methods.
powMod()#
public static function powMod(BigInteger $base, BigInteger $exponent, BigInteger $modulus): BigInteger
Computes base^exponent mod modulus in the range 0 up to the modulus. A negative exponent raises the modular inverse of the base to the absolute value of the exponent.
- Parameters:
$basethe base,$exponentthe exponent,$modulusthe modulus, must be positive - Returns:
BigInteger - Throws: InvalidModulusException if the modulus is not positive; NotInvertibleException if the exponent is negative and the base has no inverse
inverse()#
public static function inverse(BigInteger $value, BigInteger $modulus): BigInteger
Computes the number x with value * x = 1 (mod modulus) and 0 <= x < modulus. Modulo 1 the result is 0.
- Parameters:
$valuethe value to invert,$modulusthe modulus, must be positive - Returns:
BigInteger - Throws: InvalidModulusException if the modulus is not positive; NotInvertibleException if the value and the modulus are not coprime
Congruence#
A congruence: x is congruent to a remainder modulo a modulus. It is a final readonly class with the public properties remainder and modulus, both BigInteger. The remainder is normalized into the range 0 up to the modulus.
__construct()#
public function __construct(BigInteger $remainder, BigInteger $modulus)
Creates a congruence.
- Parameters:
$remainderany integer, it is normalized,$modulusthe modulus, must be positive - Throws: InvalidModulusException if the modulus is not positive
isSatisfiedBy()#
public function isSatisfiedBy(BigInteger $value): bool
Checks whether a number satisfies this congruence.
- Parameters:
$valuethe number to test - Returns:
bool
__toString()#
public function __toString(): string
Returns the congruence as text, for example x = 2 (mod 3).
- Returns:
string
ChineseRemainder#
Solves systems of simultaneous congruences. The moduli do not have to be pairwise coprime. A final class of static methods.
solve()#
public static function solve(Congruence ...$congruences): Congruence
Solves a system of congruences.
- Parameters:
...$congruencesthe congruences to satisfy at the same time - Returns:
Congruencewhere x is congruent to the remainder modulo the lcm of all moduli - Throws: EmptyCongruenceSystemException if no congruence is given; UnsolvableCongruenceException if the system is inconsistent
Primality#
Primality testing and prime search. The test is exact below DETERMINISTIC_BOUND and probabilistic from there on. The class has the constant DETERMINISTIC_BOUND, the string 3317044064679887385961981. A final class of static methods.
isPrime()#
public static function isPrime(BigInteger $number): bool
Checks whether a number is prime. Numbers below 2 are not prime. The answer is proven below DETERMINISTIC_BOUND and probabilistic from there on.
- Parameters:
$numberthe number to test - Returns:
bool
isExact()#
public static function isExact(BigInteger $number): bool
Tells whether isPrime() gives a proven answer for this number, which is the case when the number is below DETERMINISTIC_BOUND.
- Parameters:
$numberthe number that is tested - Returns:
bool
nextPrime()#
public static function nextPrime(BigInteger $number): BigInteger
Returns the smallest prime that is strictly greater than the number. Numbers below 2 give 2.
- Parameters:
$numberthe lower bound, exclusive - Returns:
BigInteger
previousPrime()#
public static function previousPrime(BigInteger $number): BigInteger
Returns the largest prime that is strictly smaller than the number.
- Parameters:
$numberthe upper bound, exclusive, must be greater than 2 - Returns:
BigInteger - Throws: InvalidNumberException if the number is not greater than 2
PrimeSieve#
Generates primes with the sieve of Eratosthenes. The sieve needs about one byte per number up to the limit. The class has the constant MAX_LIMIT, the int 10000000, which is the largest accepted limit. A final class of static methods.
primesUpTo()#
public static function primesUpTo(int $limit): array
Returns all primes less than or equal to the limit in ascending order.
- Parameters:
$limitthe inclusive upper bound, at mostMAX_LIMIT - Returns:
list<int>the primes up to and including the limit - Throws: NegativeArgumentException if the limit is negative; LimitTooLargeException if the limit is greater than
MAX_LIMIT
Factorizer#
Splits integers into their prime factors with trial division and Pollard's rho. Two close primes of 12 digits each take about 1 to 3 seconds and the time grows about ten times for every two extra digits (a rough guide), so very large semiprimes take impractically long. A final class of static methods.
factorize()#
public static function factorize(BigInteger $number): PrimeFactorization
Computes the prime factorization of a positive integer.
- Parameters:
$numberthe number to factor, must be at least 1 - Returns:
PrimeFactorization - Throws: InvalidNumberException if the number is smaller than 1
PrimeFactorization#
The prime factorization of a positive integer: a map of distinct primes to their exponents. It is a final readonly class. The factorization of 1 is empty and the primes are always in ascending order.
__construct()#
public function __construct(array $factors)
Creates a factorization from a map of primes to exponents. The prime is the key, written as a decimal string or an int.
- Parameters:
$factorsarray<int|string, int>mapping each prime to its exponent. The prime check is exact below 3317044064679887385961981 and probabilistic from there on. - Throws: InvalidNumberException if a key is not a prime or an exponent is smaller than 1
toArray()#
public function toArray(): array
Returns the map of primes to exponents. PHP turns keys that fit in an int into ints.
- Returns:
array<int|string, int>
primes()#
public function primes(): array
Returns the distinct primes in ascending order.
- Returns:
list<BigInteger>
exponentOf()#
public function exponentOf(BigInteger $prime): int
Returns the exponent of a prime, or 0 if the prime is not a factor.
- Parameters:
$primethe prime to look up - Returns:
int
distinctPrimeCount()#
public function distinctPrimeCount(): int
Returns the number of distinct primes.
- Returns:
int
isOne()#
public function isOne(): bool
Checks whether this is the empty factorization of 1.
- Returns:
bool
product()#
public function product(): BigInteger
Multiplies the prime powers back together.
- Returns:
BigInteger
divisorCount()#
public function divisorCount(): int
Returns the number of positive divisors.
- Returns:
int
divisors()#
public function divisors(): array
Returns all positive divisors in ascending order. The list has divisorCount() entries, which can be very large.
- Returns:
list<BigInteger>
totient()#
public function totient(): BigInteger
Computes Euler's totient of the number this factorization represents.
- Returns:
BigInteger
__toString()#
public function __toString(): string
Returns the factorization as text, for example 2^3 * 5. The exponent 1 is omitted and 1 is written as 1.
- Returns:
string
Totient#
Euler's totient function. A final class of static methods.
of()#
public static function of(BigInteger $number): BigInteger
Counts the integers from 1 to n that are coprime to n. The number is factored first, so the limits of Factorizer apply.
- Parameters:
$numberthe number, must be at least 1 - Returns:
BigInteger - Throws: InvalidNumberException if the number is smaller than 1
Symbols#
The Jacobi and Legendre symbols, with validation of the denominator. A final class of static methods.
jacobi()#
public static function jacobi(BigInteger $a, BigInteger $n): int
Computes the Jacobi symbol (a / n).
- Parameters:
$athe numerator, any integer,$nthe denominator, must be a positive odd integer - Returns:
int-1, 0 or 1 - Throws: InvalidNumberException if n is not a positive odd integer
legendre()#
public static function legendre(BigInteger $a, BigInteger $p): int
Computes the Legendre symbol (a / p): 1 if a is a non-zero square modulo p, -1 if it is a non-square and 0 if p divides a.
- Parameters:
$athe numerator, any integer,$pthe denominator, must be an odd prime. The prime check is exact below 3317044064679887385961981 and probabilistic from there on. - Returns:
int-1, 0 or 1 - Throws: InvalidNumberException if p is not an odd prime
Sequences#
Fibonacci numbers and factorials. A final class of static methods.
fibonacci()#
public static function fibonacci(int $n): BigInteger
Returns the n-th Fibonacci number, with F(0) = 0 and F(1) = 1.
- Parameters:
$nthe index, must not be negative - Returns:
BigInteger - Throws: NegativeArgumentException if n is negative
factorial()#
public static function factorial(int $n): BigInteger
Returns n!, with 0! = 1.
- Parameters:
$nthe number, must not be negative - Returns:
BigInteger - Throws: NegativeArgumentException if n is negative
Exceptions#
The final exception classes each implement the marker interface NumberTheoryException and have one static factory method that the library uses to create them. You normally only catch them. See Errors and exceptions for when each one is thrown.
EmptyCongruenceSystemException::create()#
public static function create(): self
Creates the exception with the message "At least one congruence is required.".
- Returns:
EmptyCongruenceSystemException
InvalidModulusException::fromModulus()#
public static function fromModulus(string $modulus): self
Creates the exception for a rejected modulus.
- Parameters:
$modulusthe decimal text of the modulus - Returns:
InvalidModulusException
InvalidNumberException::expected()#
public static function expected(string $requirement, string $value): self
Creates the exception with a description of the violated requirement.
- Parameters:
$requirementwhat the number should have been,$valuethe decimal text of the rejected number - Returns:
InvalidNumberException
LimitTooLargeException::exceeds()#
public static function exceeds(int $limit, int $maximum): self
Creates the exception for a limit above the maximum. It extends InvalidArgumentException.
- Parameters:
$limitthe rejected limit,$maximumthe largest accepted limit - Returns:
LimitTooLargeException
NegativeArgumentException::forArgument()#
public static function forArgument(string $name, int $value): self
Creates the exception for a negative argument.
- Parameters:
$namethe name of the argument,$valuethe rejected value - Returns:
NegativeArgumentException
NotInvertibleException::fromValues()#
public static function fromValues(string $value, string $modulus): self
Creates the exception for a value without an inverse. It extends DomainException.
- Parameters:
$valuethe decimal text of the value,$modulusthe decimal text of the modulus - Returns:
NotInvertibleException
UnsolvableCongruenceException::inconsistent()#
public static function inconsistent(): self
Creates the exception for an inconsistent system. It extends DomainException.
- Returns:
UnsolvableCongruenceException