phpmath / biginteger
A PHP library to work with big integers.
Requires
- php: ^8.4
- ext-gmp: *
Requires (Dev)
- phpmath/coding-standard: ^2.0
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP library to work with big integers. This library makes use of the GMP extension to do its calculations.
Documentation
The full documentation is at https://phpmath.github.io/biginteger/. Every change is listed in the changelog and every release on the releases page.
Install
composer require phpmath/biginteger
Requirements
- PHP 8.4 or newer
- The
gmpextension
Usage
Every BigInteger is an immutable value object. Operations never change the instance they are
called on, they return a new instance.
$number = new PHPMath\BigInteger\BigInteger('8273467836243255543265432745');
$a = new PHPMath\BigInteger\BigInteger('12345678901234567890'); $b = new PHPMath\BigInteger\BigInteger('98765432109876543210'); $sum = $a->add($b); echo $sum->value();
Features
This library provides a wide range of operations for working with big integers using the GMP extension:
Basic Arithmetic
- Add, subtract, multiply, and divide large numbers.
- Calculate powers and roots of numbers.
- Perform modulo operations.
- Negate numbers and get absolute values.
Comparison
- Compare numbers using
cmp()and check equality withequals(). - Determine the sign of a number (positive, negative, or zero).
Bitwise Operations
- Perform bitwise AND, OR, XOR, and NOT operations.
- Compute Hamming distance between numbers.
Number Theory
- Calculate factorials.
- Check if numbers are prime or likely prime.
- Compute modular inverses.
- Compute Jacobi, Legendre, and Kronecker symbols.
- Calculate greatest common divisors (GCD) and least common multiples (LCM).
- Check for perfect squares and perfect powers.
- Find the next prime number.
- Compute binomial coefficients.
Development
composer test
composer test-coverage
composer analyse
composer check-style
composer fix-style
Contributing
All contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.
Security
If you discover a security issue, please create an issue in the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.