|
ADS
Algorithms and Data Structures
|
an integer of any length, sign and magnitude in base \( 2^{64} \) More...
#include <ADS/numeric/BigInteger.h>
Public types | |
| using | Limb = std::uint64_t |
| a digit, in base \( 2^{64} \) | |
Public member functions | |
| BigInteger (long long v=0) | |
| the number \( v \) | |
| BigInteger (const std::string &s, int base=10) | |
| the number written in \( s \) in the base, with an optional sign | |
| std::string | toString (int base=10) const |
| the figures of the number in the base, with a sign if negative | |
| std::string | toStringSchool (int base=10) const |
| the figures of toString() by the code of the book, with no backend, \( O( n^2 ) \) | |
| int | sign (void) const |
| the sign, \( -1 \), 0 or 1 | |
| BigInteger | abs (void) const |
| the absolute value \( |x| \) | |
| std::size_t | limbs (void) const |
| the number of digits (limbs) in base \( 2^{64} \) | |
| BigInteger | operator- (void) const |
| the opposite \( -x \) | |
| BigInteger & | operator+= (const BigInteger &b) |
| the sum \( x + b \) into \( x \) | |
| BigInteger & | operator-= (const BigInteger &b) |
| the difference \( x - b \) into \( x \) | |
| BigInteger & | operator*= (const BigInteger &b) |
| the product \( x b \) into \( x \) | |
Static public member functions | |
| static BigInteger | fromStringSchool (const std::string &s, int base=10) |
| the number of the constructor from a string by the code of the book, with no backend, \( O( n^2 ) \): each chunk of \( c \) figures is \( x \gets x P + \mathit{chunk} \) (section 4.4) | |
| static std::pair< BigInteger, BigInteger > | divMod (const BigInteger &a, const BigInteger &b) |
| the quotient truncated toward zero and the remainder, by Algorithm D [see divModSchool()] or by the backend | |
| static std::pair< BigInteger, BigInteger > | divModSchool (const BigInteger &a, const BigInteger &b) |
| the quotient and the remainder by Algorithm D, \( O( m n ) \) | |
| static BigInteger | multiplySchool (const BigInteger &a, const BigInteger &b) |
| the product by Algorithm M, \( O( n_a n_b ) \) | |
| static BigInteger | multiplyKaratsuba (const BigInteger &a, const BigInteger &b) |
| the product by Karatsuba at every length down to factors of fewer than 4 digits, which Algorithm M multiplies, never by the NTT | |
| static BigInteger | multiplyFFT (const BigInteger &a, const BigInteger &b) |
| the product by the number theoretic transform at every length | |
| static BigInteger | divExact (BigInteger a, const BigInteger &b) |
| the quotient \( a / b \) for \( b \) a divisor of \( a \), by mpz_divexact() with the backend, else as \( a / b \) | |
| static void | addMul (BigInteger &r, const BigInteger &a, const BigInteger &b) |
| \( r \gets r + a b \) | |
| static BigInteger | pow (const BigInteger &x, unsigned long n) |
| the power \( x^n \), by mpz_pow_ui() with the backend, else by the binary method | |
| static std::pair< BigInteger, BigInteger > | sqrtRem (const BigInteger &a) |
| the integer square root \( s = \lfloor \sqrt{a} \rfloor \) and \( r = a - s^2 \): \( s \) by mpn_sqrtrem() with the backend, else by sqrtNewton() | |
| static BigInteger | sqrt (const BigInteger &a) |
| the root \( \lfloor \sqrt{a} \rfloor \) of sqrtRem(), with no remainder | |
| static BigInteger | sqrtNewton (const BigInteger &a) |
| the integer square root by Newton's method from above | |
| static BigInteger | gcd (const BigInteger &a, const BigInteger &b) |
| the greatest common divisor of \( |a| \) and \( |b| \), by mpz_gcd() with the backend, else by Lehmer | |
| static BigInteger | gcdEuclid (BigInteger a, BigInteger b) |
| the greatest common divisor of \( |a| \) and \( |b| \), by Euclid | |
Static public attributes | |
| static constexpr std::size_t | kKaratsuba = 32 |
| limbs for Karatsuba | |
| static constexpr std::size_t | kFFT = 4096 |
| limbs for the NTT | |
Friends | |
| class | Rational |
| BigInteger | operator+ (const BigInteger &a, const BigInteger &b) |
| the sum \( a + b \), by Algorithm 4.3.1A on the magnitudes of the same sign and 4.3.1S on those of opposite signs, \( O( n ) \) | |
| BigInteger | operator- (const BigInteger &a, const BigInteger &b) |
| the difference \( a - b \), as the sum with the sign of \( b \) changed | |
| BigInteger | operator* (const BigInteger &a, const BigInteger &b) |
| the product \( a b \), by the method of the length of the factors | |
| BigInteger | operator/ (const BigInteger &a, const BigInteger &b) |
| the quotient \( a / b \) truncated toward zero | |
| BigInteger | operator% (const BigInteger &a, const BigInteger &b) |
| the remainder of divMod(), of the sign of \( a \) | |
| bool | operator== (const BigInteger &a, const BigInteger &b)=default |
| true if the two numbers are equal | |
| std::strong_ordering | operator<=> (const BigInteger &a, const BigInteger &b) |
| the order of \( a \) and \( b \), by the signs, the lengths and the digits from the most significant | |
an integer of any length, sign and magnitude in base \( 2^{64} \)
| using ADS::BigInteger::Limb = std::uint64_t |
a digit, in base \( 2^{64} \)
| ADS::BigInteger::BigInteger | ( | long long | v = 0 | ) |
the number \( v \)
|
explicit |
the number written in \( s \) in the base, with an optional sign
In pseudocode (section 4.4), \( P = \beta^c \) the largest power of the base in a digit:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fromString}( s , \beta ) \cr 2 & \quad \textbf{if}\ \beta \text{ is a power of 2}\ \textbf{then} \ \textbf{return}\ \text{the bits of the figures, side by side} \cr 3 & \quad x \gets 0 \cr 4 & \quad \textbf{foreach}\ \text{chunk } t \text{ of up to } c \text{ figures, from the left}\ \textbf{do}\ x \gets x \beta^{|t|} + t \cr 5 & \quad \textbf{return}\ x \end{array}} \]
|
nodiscard |
the absolute value \( |x| \)
|
static |
\( r \gets r + a b \)
|
static |
the quotient \( a / b \) for \( b \) a divisor of \( a \), by mpz_divexact() with the backend, else as \( a / b \)
|
static |
the quotient truncated toward zero and the remainder, by Algorithm D [see divModSchool()] or by the backend
|
static |
the quotient and the remainder by Algorithm D, \( O( m n ) \)
In pseudocode (Algorithm 4.3.1D), in base \( \beta = 2^{64} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{divModSchool}( u , v ) \cr 2 & \quad \text{shift } u \text{ and } v \text{ left until the top bit } \text{of } v_{n-1} \text{ is 1} \cr 3 & \quad \textbf{for}\ j \gets m\ \textbf{downto}\ 0\ \textbf{do} \cr 4 & \qquad \hat q \gets \min\{ \lfloor ( u_{j+n} \beta + u_{j+n-1} ) / v_{n-1} \rfloor , \beta - 1 \};\ \hat r \gets u_{j+n} \beta + u_{j+n-1} - \hat q v_{n-1} \cr 5 & \qquad \textbf{while}\ \hat r < \beta\ \textbf{and}\ \hat q v_{n-2} > \hat r \beta + u_{j+n-2}\ \textbf{do}\ \hat q \gets \hat q - 1; \ \hat r \gets \hat r + v_{n-1} \cr 6 & \qquad u_{j .. j+n} \gets u_{j .. j+n} - \hat q v \cr 7 & \qquad \textbf{if}\ \text{it is negative}\ \textbf{then}\ \hat q \gets \hat q - 1;\ u_{j .. j+n} \gets u_{j .. j+n} + v \cr 8 & \qquad q_j \gets \hat q \cr 9 & \quad \textbf{return}\ ( q , u \text{ shifted back right} ) \end{array}} \]
|
static |
the number of the constructor from a string by the code of the book, with no backend, \( O( n^2 ) \): each chunk of \( c \) figures is \( x \gets x P + \mathit{chunk} \) (section 4.4)
|
static |
the greatest common divisor of \( |a| \) and \( |b| \), by mpz_gcd() with the backend, else by Lehmer
In pseudocode (Algorithm 4.5.2L), \( \hat u , \hat v \) the leading 60 bits of \( u \ge v \) at the same place:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gcd}( u , v ) \cr 2 & \quad \textbf{while}\ v \text{ has two digits or more}\ \textbf{do} \cr 3 & \qquad ( A , B , C , D ) \gets ( 1 , 0 , 0 , 1 ) \cr 4 & \qquad \textbf{while}\ \hat v + C \ne 0\ \textbf{and}\ \hat v + D \ne 0\ \textbf{do} \cr 5 & \qquad\quad q \gets \lfloor ( \hat u + A ) / ( \hat v + C ) \rfloor; \ \textbf{if}\ q \ne \lfloor ( \hat u + B ) / ( \hat v + D ) \rfloor\ \textbf{then break} \cr 6 & \qquad\quad ( A , B , C , D , \hat u , \hat v ) \gets ( C , D , A - q C , B - q D , \hat v , \hat u - q \hat v ) \cr 7 & \qquad \textbf{if}\ B = 0\ \textbf{then}\ ( u , v ) \gets ( v , u \bmod v ) \cr 8 & \qquad \textbf{else}\ ( u , v ) \gets ( A u + B v , C u + D v ) \cr 9 & \quad \textbf{return}\ \text{the binary gcd of the last digits} \end{array}} \]
|
static |
the greatest common divisor of \( |a| \) and \( |b| \), by Euclid
In pseudocode (Algorithm 4.5.2A):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gcdEuclid}( a , b ) \cr 2 & \quad a \gets |a|;\ b \gets |b| \cr 3 & \quad \textbf{while}\ b \ne 0\ \textbf{do}\ ( a , b ) \gets ( b , a \bmod b ) \cr 4 & \quad \textbf{return}\ a \end{array}} \]
|
nodiscard |
the number of digits (limbs) in base \( 2^{64} \)
|
static |
the product by the number theoretic transform at every length
In pseudocode (sections 4.3.3 and 4.6.4), \( p_1 , p_2 , p_3 \) primes with roots of unity of the length:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyFFT}( a , b ) \cr 2 & \quad \textbf{foreach}\ p \in \{ p_1 , p_2 , p_3 \}\ \textbf{do} \cr 3 & \qquad c^{( p )} \gets \text{the cyclic convolution of the pieces } \text{of 32 bits modulo } p \cr 4 & \quad c \gets \text{the coefficients from the } c^{( p )} \text{ by } \text{the Chinese remainder theorem} \cr 5 & \quad \textbf{return}\ \sum_i c_i 2^{32 i} \text{, by the carries} \end{array}} \]
where each convolution is the product of the transforms, of length \( N \) a power of 2, taken in place and without recursion, with \( g \) a generator of the units modulo \( p \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{transform}( a , \mathit{inverse} ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ N - 1\ \textbf{do} \ \textbf{if}\ i < \mathrm{rev}( i )\ \textbf{then}\ \text{swap } a_i , a_{\mathrm{rev}( i )}\quad \triangleright\ \text{the bits } \text{of } i \text{ reversed} \cr 3 & \quad \textbf{for}\ \ell \gets 2 , 4 , \ldots , N\ \textbf{do} \cr 4 & \qquad \omega \gets g^{( p - 1 ) / \ell} \bmod p;\ \textbf{if} \ \mathit{inverse}\ \textbf{then}\ \omega \gets \omega^{-1} \cr 5 & \qquad \textbf{for}\ i \gets 0 , \ell , 2 \ell , \ldots , N - \ell \ \textbf{do} \cr 6 & \qquad\quad \textbf{for}\ k \gets 0\ \textbf{to}\ \ell / 2 - 1 \ \textbf{do}\quad \triangleright\ \text{a butterfly} \cr 7 & \qquad\qquad u \gets a_{i+k};\ v \gets \omega^k a_{i+k+\ell/2} \bmod p;\ a_{i+k} \gets u + v \bmod p;\ a_{i+k+\ell/2} \gets u - v \bmod p \cr 8 & \quad \textbf{if}\ \mathit{inverse}\ \textbf{then}\ a_i \gets N^{-1} a_i \bmod p \text{ for each } i \end{array}} \]
|
static |
the product by Karatsuba at every length down to factors of fewer than 4 digits, which Algorithm M multiplies, never by the NTT
In pseudocode (section 4.3.3), \( n_a \ge n_b \), \( l = \lceil n_a / 2 \rceil \), \( a = a_1 \beta^l + a_0 \) and \( b = b_1 \beta^l + b_0 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{karatsuba}( a , b ) \cr 2 & \quad \textbf{if}\ n_b < 4\ \textbf{then return} \ \mathrm{multiplySchool}( a , b ) \cr 3 & \quad \textbf{if}\ n_b \le l\ \textbf{then return} \ \mathrm{karatsuba}( a_1 , b ) \beta^l + \mathrm{karatsuba}( a_0 , b ) \cr 4 & \quad z_0 \gets a_0 b_0;\ z_2 \gets a_1 b_1;\ z_1 \gets ( a_0 + a_1 ) ( b_0 + b_1 ) - z_0 - z_2 \cr 5 & \quad \textbf{return}\ z_2 \beta^{2l} + z_1 \beta^l + z_0 \end{array}} \]
|
static |
the product by Algorithm M, \( O( n_a n_b ) \)
In pseudocode (Algorithm 4.3.1M), in base \( \beta = 2^{64} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplySchool}( a , b ) \cr 2 & \quad w \gets n_a + n_b \text{ zero digits} \cr 3 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ n_b - 1\ \textbf{do} \cr 4 & \qquad c \gets 0 \cr 5 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ n_a - 1\ \textbf{do} \ ( c , w_{i+j} ) \gets a_i b_j + w_{i+j} + c \cr 6 & \qquad w_{j+n_a} \gets c \cr 7 & \quad \textbf{return}\ w \text{ with the sign of } a b \end{array}} \]
| BigInteger & ADS::BigInteger::operator*= | ( | const BigInteger & | b | ) |
the product \( x b \) into \( x \)
| BigInteger & ADS::BigInteger::operator+= | ( | const BigInteger & | b | ) |
the sum \( x + b \) into \( x \)
| BigInteger ADS::BigInteger::operator- | ( | void | ) | const |
the opposite \( -x \)
| BigInteger & ADS::BigInteger::operator-= | ( | const BigInteger & | b | ) |
the difference \( x - b \) into \( x \)
|
static |
the power \( x^n \), by mpz_pow_ui() with the backend, else by the binary method
In pseudocode (section 4.6.3), from the leftmost bit of \( n \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pow}( x , n ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ 1 \cr 3 & \quad y \gets x \cr 4 & \quad \textbf{foreach}\ \text{bit } \nu \text{ of } n \text{ after } \text{its leftmost 1, from the left}\ \textbf{do} \cr 5 & \qquad y \gets y^2;\ \textbf{if}\ \nu = 1\ \textbf{then}\ y \gets y x \cr 6 & \quad \textbf{return}\ y \end{array}} \]
|
nodiscard |
the sign, \( -1 \), 0 or 1
|
static |
the root \( \lfloor \sqrt{a} \rfloor \) of sqrtRem(), with no remainder
|
static |
the integer square root by Newton's method from above
In pseudocode (Newton's method on the integers), the iterates decreasing to \( \lfloor \sqrt{a} \rfloor \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sqrtNewton}( a ) \cr 2 & \quad \textbf{if}\ a = 0\ \textbf{then return}\ 0 \cr 3 & \quad x \gets 2^{\lceil \ell / 2 \rceil}\quad \triangleright\ \ell \text{ the bits of } a \cr 4 & \quad \textbf{loop} \cr 5 & \qquad y \gets \lfloor ( x + \lfloor a / x \rfloor ) / 2 \rfloor \cr 6 & \qquad \textbf{if}\ y \ge x\ \textbf{then return}\ x \cr 7 & \qquad x \gets y \end{array}} \]
|
static |
the integer square root \( s = \lfloor \sqrt{a} \rfloor \) and \( r = a - s^2 \): \( s \) by mpn_sqrtrem() with the backend, else by sqrtNewton()
|
nodiscard |
the figures of the number in the base, with a sign if negative
In pseudocode (section 4.4), \( P = \beta^c \) the largest power of the base in a digit:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{toString}( x , \beta ) \cr 2 & \quad \textbf{if}\ \beta \text{ is a power of 2}\ \textbf{then} \ \textbf{return}\ \text{the slices of bits of } x \cr 3 & \quad \textbf{while}\ x \ne 0\ \textbf{do} \cr 4 & \qquad ( x , r ) \gets \mathrm{divMod}( x , P ) \text{, a division } \text{by one digit} \cr 5 & \qquad \text{prepend the } c \text{ figures of } r \text{ in the base } \beta \cr 6 & \quad \textbf{return}\ \text{the figures, the zeros on the left } \text{removed} \end{array}} \]
|
nodiscard |
the figures of toString() by the code of the book, with no backend, \( O( n^2 ) \)
|
friend |
the remainder of divMod(), of the sign of \( a \)
|
friend |
the product \( a b \), by the method of the length of the factors
In pseudocode (sections 4.3.1 and 4.3.3), \( n_a \ge n_b \) the digits of the factors:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiply}( a , b ) \cr 2 & \quad \textbf{if}\ n_b < \mathtt{kKaratsuba}\ \textbf{then return}\ \mathrm{multiplySchool}( a , b ) \cr 3 & \quad \textbf{if}\ n_b \ge \mathtt{kFFT}\ \textbf{then return} \ \text{the product by the NTT} \cr 4 & \quad \textbf{return}\ \text{the product by Karatsuba, each } \text{part multiplied again} \end{array}} \]
|
friend |
the sum \( a + b \), by Algorithm 4.3.1A on the magnitudes of the same sign and 4.3.1S on those of opposite signs, \( O( n ) \)
|
friend |
the difference \( a - b \), as the sum with the sign of \( b \) changed
|
friend |
the quotient \( a / b \) truncated toward zero
|
friend |
the order of \( a \) and \( b \), by the signs, the lengths and the digits from the most significant
|
friend |
true if the two numbers are equal
|
friend |
|
staticconstexpr |
limbs for the NTT
|
staticconstexpr |
limbs for Karatsuba