snippets 0.1.0
|
Implements a class for representing and manipulating large integers beyond the native integer range. More...
#include <big_integer.hpp>
Public Member Functions | |
std::string | to_string () const |
template<std::integral T> | |
std::optional< T > | to () const |
void | negate () |
BigInteger | abs () const |
BigInteger | operator- () const |
BigInteger | operator+ (const BigInteger &other) const |
BigInteger | operator- (const BigInteger &other) const |
BigInteger | operator* (const BigInteger &other) const |
BigInteger | operator/ (const BigInteger &other) const |
BigInteger & | operator+= (const BigInteger &other) |
BigInteger & | operator-= (const BigInteger &other) |
BigInteger & | operator*= (const BigInteger &other) |
BigInteger & | operator/= (const BigInteger &other) |
BigInteger | operator% (const BigInteger &other) const |
std::pair< BigInteger, BigInteger > | divmod (const BigInteger &other) const |
BigInteger & | operator++ () |
BigInteger | operator++ (int) |
BigInteger & | operator-- () |
BigInteger | operator-- (int) |
bool | operator== (const BigInteger &other) const |
bool | operator!= (const BigInteger &other) const |
bool | operator< (const BigInteger &other) const |
bool | operator<= (const BigInteger &other) const |
bool | operator> (const BigInteger &other) const |
bool | operator>= (const BigInteger &other) const |
void | multiplyByPowerOfTen (std::size_t power) |
void | divideByPowerOfTen (std::size_t power) |
Static Public Member Functions | |
static BigInteger | parse (const std::string &number) |
template<std::integral T> | |
static BigInteger | from_integer (T number) |
static const BigInteger & | zero () |
static const BigInteger & | one () |
static const BigInteger & | two () |
template<std::integral T> | |
static const BigInteger & | getMinValueInstance () |
template<std::integral T> | |
static const BigInteger & | getMaxValueInstance () |
static BigInteger | max (const BigInteger &a, const BigInteger &b) |
static BigInteger | min (const BigInteger &a, const BigInteger &b) |
static BigInteger | gcd (const BigInteger &a, const BigInteger &b) |
static BigInteger | lcm (const BigInteger &a, const BigInteger &b) |
static BigInteger | factorial (unsigned int n) |
static BigInteger | pow (const BigInteger &a, unsigned int n) |
static BigInteger | sqrt (const BigInteger &number) |
static BigInteger | log2 (const BigInteger &number) |
static BigInteger | log10 (const BigInteger &number) |
Implements a class for representing and manipulating large integers beyond the native integer range.
This class supports basic arithmetic operations, comparison, and conversion from/to strings and native integer types. It handles both positive and negative large integer values.
|
inlinenodiscard |
Calculates the absolute value of the BigInteger instance.
|
inline |
Divides the BigInteger by a power of 10. This operation is optimized for base-10 representations and is performed by removing the specified number of least significant digits from the number. If the power exceeds the number of digits, the result is set to 0.
power | The exponent of 10 by which to divide the BigInteger. For example, a power of 2 means dividing the BigInteger by 100. |
|
inlinenodiscard |
Performs division and modulo operations simultaneously, returning both the quotient and the remainder.
This method divides this BigInteger by another BigInteger (divisor) and returns a pair consisting of the quotient and the remainder. It is more efficient than performing division and modulo operations separately. If the divisor is zero, the method throws a runtime error due to division by zero being undefined.
The remainder's sign is adjusted to match the divisor's sign, following the standard mathematical convention for division and modulus operations. This ensures that the remainder has the same sign as the divisor or is non-negative if the divisor is positive.
other | The divisor in the division and modulo operations. |
std::runtime_error | If attempting division by zero in the divmod operation. |
|
inlinestatic |
Calculates the factorial of a non-negative integer.
n | The non-negative integer for which to compute the factorial. |
|
inlinestatic |
Constructs a BigInteger from an integral type.
T | The integral type of the input number. |
number | The number to be converted to a BigInteger. |
|
inlinestatic |
Calculates the Greatest Common Divisor (GCD) of two BigInteger values using the Euclidean algorithm.
The Euclidean algorithm is based on the principle that the gcd of two numbers also divides their difference. This method repeatedly replaces the larger number by its difference with the smaller number until one of the numbers becomes zero. The non-zero number at this point is the gcd of the original two numbers.
a | The first BigInteger for which to find the gcd. |
b | The second BigInteger for which to find the gcd. |
|
inlinestatic |
Provides a singleton instance representing the maximum value of an integral type.
T | The integral type for which the maximum value is requested. |
|
inlinestatic |
Provides a singleton instance representing the minimum value of an integral type.
T | The integral type for which the minimum value is requested. |
|
inlinestatic |
Calculates the Least Common Multiple (LCM) of two BigInteger values using the formula lcm(a, b) = |a * b| / gcd(a, b).
The lcm of two numbers is calculated using their gcd with the above formula. This works because the gcd of two numbers also divides their lcm. The method ensures the result is non-negative, as the lcm is defined as a non-negative value. In case both a and b are zero, the lcm is defined to be zero.
a | The first BigInteger for which to find the lcm. |
b | The second BigInteger for which to find the lcm. |
|
inlinestatic |
Calculates the base-10 logarithm of a BigInteger.
number | The BigInteger for which to calculate the base-10 logarithm. |
std::out_of_range | If the input is zero (log10(0) is undefined) or negative (log10 of a negative number is not allowed). |
|
inlinestatic |
Calculates the base-2 logarithm of a BigInteger.
number | The BigInteger for which to calculate the base-2 logarithm. |
std::out_of_range | If the input is zero (log2(0) is undefined) or negative (log2 of a negative number is not allowed). |
|
inlinestatic |
Determines the maximum of two BigInteger values.
a | The first BigInteger to compare. |
b | The second BigInteger to compare. |
|
inlinestatic |
Determines the minimum of two BigInteger values.
a | The first BigInteger to compare. |
b | The second BigInteger to compare. |
|
inline |
Multiplies the BigInteger by a power of 10. This operation is optimized for base-10 representations and is performed by simply adding the specified number of zeros to the least significant digits of the number.
power | The exponent of 10 by which to multiply the BigInteger. For example, a power of 3 means multiplying the BigInteger by 1000. |
|
inline |
|
inlinestatic |
Provides a singleton instance representing one.
|
inline |
|
inline |
Calculates the remainder of division of this BigInteger by another BigInteger.
The modular operation follows the standard definition where the remainder of the division of this BigInteger (dividend) by the 'other' BigInteger (divisor) is returned. If the divisor is zero, the operation throws a runtime error due to division by zero being undefined.
other | The divisor in the modular operation. |
std::runtime_error | If attempting to perform modulo by zero. |
|
inline |
Multiplies this BigInteger with another BigInteger.
The method uses a classic grade-school multiplication algorithm, where each digit of the first number is multiplied by each digit of the second number, and the intermediate results are added together, taking care of the carry at each step. The result is stored in a temporary vector which is large enough to hold the maximum possible number of digits (sum of the number of digits in both numbers).
Time Complexity: O(n*m), where n and m are the number of digits in the two numbers. This is because each digit of the first number is multiplied with each digit of the second number. Space Complexity: O(n + m), which is the size of the temporary vector used to store the intermediate results.
other | The BigInteger to multiply with this BigInteger. |
|
inline |
Multiplies the current BigInteger by another BigInteger and assigns the result to the current object.
other | The BigInteger to multiply with the current BigInteger. |
|
inline |
Adds two BigInteger instances.
other | The BigInteger instance to add to the current instance. |
|
inline |
|
inline |
|
inline |
Adds the value of another BigInteger to this instance and updates this instance with the result.
This operator modifies the current instance by adding the value of the specified BigInteger ('other') to it. The addition is performed using the existing addition logic defined in the class, which handles the arithmetic and any necessary carry operation. After the addition, the current instance is updated with the new value, making this operation in-place.
other | The BigInteger to add to this instance. |
|
inline |
|
inline |
Subtracts a BigInteger instance from the current instance.
other | The BigInteger instance to subtract from the current instance. |
|
inline |
|
inline |
|
inline |
Subtracts the value of another BigInteger from this instance and updates this instance with the result.
This operator modifies the current instance by subtracting the value of the specified BigInteger ('other') from it. The subtraction is performed using the existing subtraction logic defined in the class, which handles the arithmetic and any necessary borrow operation. After the subtraction, the current instance is updated with the new value, making this operation in-place.
other | The BigInteger to subtract from this instance. |
|
inline |
Divides this BigInteger by another BigInteger and returns the quotient.
The method uses a long division approach where the dividend is divided by the divisor starting from the highest order of magnitude down to the lowest. At each step, the method finds how many times the divisor fits into the current portion of the dividend and subtracts the corresponding multiple of the divisor from the dividend. The quotient is built digit by digit from these multiples.
Time Complexity: For simplicity, let's denote this as O(d*(n-m)), where n is the number of digits in the dividend, m is the number of digits in the divisor, and d is the number of digits in the quotient. This accounts for the repeated subtraction of the divisor from portions of the dividend. Space Complexity: O(n), mainly for storing the quotient, where n is the number of digits in the dividend.
other | The BigInteger divisor. |
std::runtime_error | if attempted to divide by zero. |
|
inline |
Divides the current BigInteger by another BigInteger and assigns the result to the current object.
other | The BigInteger to divide the current BigInteger by. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inlinestatic |
Constructs a BigInteger from a string representation.
number | A string representing a potentially large integer. |
std::invalid_argument | If the string contains non-numeric characters (excluding an initial minus sign). |
|
inlinestatic |
Computes a raised to the power of n using the fast powering algorithm.
a | The base as a BigInteger. |
n | The exponent as an unsigned integer. |
|
inlinestatic |
Calculates the square root of a BigInteger.
number | The BigInteger to find the square root of. |
std::runtime_error | if the given BigInteger is negative. |
|
inline |
Attempts to convert the BigInteger to a specified integral type T.
This method checks if the BigInteger's value fits within the range of the target type T. If the conversion is possible, the method returns an std::optional containing the converted value. If the conversion would lead to overflow, underflow, or if the BigInteger's value cannot be accurately represented by type T, the method returns std::nullopt.
T | The target integral type for the conversion. Must satisfy std::integral. |
|
inlinenodiscard |
Converts the BigInteger to its string representation.
|
inlinestatic |
Provides a singleton instance representing two.
|
inlinestatic |
Provides a singleton instance representing zero.