123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761 |
- /*
- BigInt
- ------
- Arbitrary-sized integer class for C++.
-
- Version: 0.4.0-dev
- Released on: 03 January 2018 10:51 IST
- Author: Syed Faheel Ahmad (faheel@live.in)
- Project on GitHub: https://github.com/faheel/BigInt
- License: MIT
- */
- /*
- ===========================================================================
- BigInt
- ===========================================================================
- Definition for the BigInt class.
- */
- #ifndef BIG_INT_HPP
- #define BIG_INT_HPP
- #include <iostream>
- class BigInt {
- std::string value;
- char sign;
- public:
- // Constructors:
- BigInt();
- BigInt(const BigInt&);
- BigInt(const long long&);
- BigInt(const std::string&);
- // Assignment operators:
- BigInt& operator=(const BigInt&);
- BigInt& operator=(const long long&);
- BigInt& operator=(const std::string&);
- // Unary arithmetic operators:
- BigInt operator+() const; // unary +
- BigInt operator-() const; // unary -
- // Binary arithmetic operators:
- BigInt operator+(const BigInt&) const;
- BigInt operator-(const BigInt&) const;
- BigInt operator*(const BigInt&) const;
- BigInt operator/(const BigInt&) const;
- BigInt operator%(const BigInt&) const;
- BigInt operator+(const long long&) const;
- BigInt operator-(const long long&) const;
- BigInt operator*(const long long&) const;
- BigInt operator/(const long long&) const;
- BigInt operator%(const long long&) const;
- BigInt operator+(const std::string&) const;
- BigInt operator-(const std::string&) const;
- BigInt operator*(const std::string&) const;
- BigInt operator/(const std::string&) const;
- BigInt operator%(const std::string&) const;
- // Arithmetic-assignment operators:
- BigInt& operator+=(const BigInt&);
- BigInt& operator-=(const BigInt&);
- BigInt& operator*=(const BigInt&);
- BigInt& operator/=(const BigInt&);
- BigInt& operator%=(const BigInt&);
- BigInt& operator+=(const long long&);
- BigInt& operator-=(const long long&);
- BigInt& operator*=(const long long&);
- BigInt& operator/=(const long long&);
- BigInt& operator%=(const long long&);
- BigInt& operator+=(const std::string&);
- BigInt& operator-=(const std::string&);
- BigInt& operator*=(const std::string&);
- BigInt& operator/=(const std::string&);
- BigInt& operator%=(const std::string&);
- // Increment and decrement operators:
- BigInt& operator++(); // pre-increment
- BigInt& operator--(); // pre-decrement
- BigInt operator++(int); // post-increment
- BigInt operator--(int); // post-decrement
- // Relational operators:
- bool operator<(const BigInt&) const;
- bool operator>(const BigInt&) const;
- bool operator<=(const BigInt&) const;
- bool operator>=(const BigInt&) const;
- bool operator==(const BigInt&) const;
- bool operator!=(const BigInt&) const;
- bool operator<(const long long&) const;
- bool operator>(const long long&) const;
- bool operator<=(const long long&) const;
- bool operator>=(const long long&) const;
- bool operator==(const long long&) const;
- bool operator!=(const long long&) const;
- bool operator<(const std::string&) const;
- bool operator>(const std::string&) const;
- bool operator<=(const std::string&) const;
- bool operator>=(const std::string&) const;
- bool operator==(const std::string&) const;
- bool operator!=(const std::string&) const;
- // I/O stream operators:
- friend std::istream& operator>>(std::istream&, BigInt&);
- friend std::ostream& operator<<(std::ostream&, const BigInt&);
- // Conversion functions:
- std::string to_string() const;
- int to_int() const;
- long to_long() const;
- long long to_long_long() const;
- };
- #endif // BIG_INT_HPP
- /*
- ===========================================================================
- Utility functions
- ===========================================================================
- */
- #ifndef BIG_INT_UTILITY_FUNCTIONS_HPP
- #define BIG_INT_UTILITY_FUNCTIONS_HPP
- #include <tuple>
- /*
- is_valid_number
- ---------------
- Checks whether the given string is a valid integer.
- */
- bool is_valid_number(const std::string& num) {
- for (char digit : num)
- if (digit < '0' or digit > '9')
- return false;
- return true;
- }
- /*
- strip_leading_zeroes
- --------------------
- Strip the leading zeroes from a number represented as a string.
- */
- void strip_leading_zeroes(std::string& num) {
- size_t i;
- for (i = 0; i < num.size(); i++)
- if (num[i] != '0')
- break;
- if (i == num.size())
- num = "0";
- else
- num = num.substr(i);
- }
- /*
- add_leading_zeroes
- ------------------
- Adds a given number of leading zeroes to a string-represented integer `num`.
- */
- void add_leading_zeroes(std::string& num, size_t num_zeroes) {
- num = std::string(num_zeroes, '0') + num;
- }
- /*
- add_trailing_zeroes
- -------------------
- Adds a given number of trailing zeroes to a string-represented integer `num`.
- */
- void add_trailing_zeroes(std::string& num, size_t num_zeroes) {
- num += std::string(num_zeroes, '0');
- }
- /*
- get_larger_and_smaller
- ----------------------
- Identifies the given string-represented integers as `larger` and `smaller`,
- padding the smaller number with leading zeroes to make it equal in length to
- the larger number.
- */
- std::tuple<std::string, std::string> get_larger_and_smaller(const std::string& num1,
- const std::string& num2) {
- std::string larger, smaller;
- if (num1.size() > num2.size() or
- (num1.size() == num2.size() and num1 > num2)) {
- larger = num1;
- smaller = num2;
- }
- else {
- larger = num2;
- smaller = num1;
- }
- // pad the smaller number with zeroes
- add_leading_zeroes(smaller, larger.size() - smaller.size());
- return std::make_tuple(larger, smaller);
- }
- #endif // BIG_INT_UTILITY_FUNCTIONS_HPP
- /*
- ===========================================================================
- Constructors
- ===========================================================================
- */
- #ifndef BIG_INT_CONSTRUCTORS_HPP
- #define BIG_INT_CONSTRUCTORS_HPP
- /*
- Default constructor
- -------------------
- */
- BigInt::BigInt() {
- value = "0";
- sign = '+';
- }
- /*
- Copy constructor
- ----------------
- */
- BigInt::BigInt(const BigInt& num) {
- value = num.value;
- sign = num.sign;
- }
- /*
- Integer to BigInt
- -----------------
- */
- BigInt::BigInt(const long long& num) {
- value = std::to_string(num);
- if (num < 0) {
- sign = '-';
- value = value.substr(1); // remove minus sign from value
- }
- else
- sign = '+';
- }
- /*
- String to BigInt
- ----------------
- */
- BigInt::BigInt(const std::string& num) {
- if (num[0] == '+' or num[0] == '-') { // check for sign
- std::string magnitude = num.substr(1);
- if (is_valid_number(magnitude)) {
- value = magnitude;
- sign = num[0];
- }
- else {
- throw std::invalid_argument("Expected an integer, got \'" + num + "\'");
- }
- }
- else { // if no sign is specified
- if (is_valid_number(num)) {
- value = num;
- sign = '+'; // positive by default
- }
- else {
- throw std::invalid_argument("Expected an integer, got \'" + num + "\'");
- }
- }
- strip_leading_zeroes(value);
- }
- #endif // BIG_INT_CONSTRUCTORS_HPP
- /*
- ===========================================================================
- Conversion functions for BigInt
- ===========================================================================
- */
- #ifndef BIG_INT_CONVERSION_FUNCTIONS_HPP
- #define BIG_INT_CONVERSION_FUNCTIONS_HPP
- /*
- to_string
- ---------
- Converts a BigInt to a string.
- */
- std::string BigInt::to_string() const {
- // prefix with sign if negative
- return this->sign == '-' ? "-" + this->value : this->value;
- }
- /*
- to_int
- ------
- Converts a BigInt to an int.
- NOTE: If the BigInt is out of range of an int, stoi() will throw an
- out_of_range exception.
- */
- int BigInt::to_int() const {
- return std::stoi(this->to_string());
- }
- /*
- to_long
- -------
- Converts a BigInt to a long int.
- NOTE: If the BigInt is out of range of a long int, stol() will throw an
- out_of_range exception.
- */
- long BigInt::to_long() const {
- return std::stol(this->to_string());
- }
- /*
- to_long_long
- ------------
- Converts a BigInt to a long long int.
- NOTE: If the BigInt is out of range of a long long int, stoll() will throw
- an out_of_range exception.
- */
- long long BigInt::to_long_long() const {
- return std::stoll(this->to_string());
- }
- #endif // BIG_INT_CONVERSION_FUNCTIONS_HPP
- /*
- ===========================================================================
- Assignment operators
- ===========================================================================
- */
- #ifndef BIG_INT_ASSIGNMENT_OPERATORS_HPP
- #define BIG_INT_ASSIGNMENT_OPERATORS_HPP
- /*
- BigInt = BigInt
- ---------------
- */
- BigInt& BigInt::operator=(const BigInt& num) {
- value = num.value;
- sign = num.sign;
- return *this;
- }
- /*
- BigInt = Integer
- ----------------
- */
- BigInt& BigInt::operator=(const long long& num) {
- BigInt temp(num);
- value = temp.value;
- sign = temp.sign;
- return *this;
- }
- /*
- BigInt = String
- ---------------
- */
- BigInt& BigInt::operator=(const std::string& num) {
- BigInt temp(num);
- value = temp.value;
- sign = temp.sign;
- return *this;
- }
- #endif // BIG_INT_ASSIGNMENT_OPERATORS_HPP
- /*
- ===========================================================================
- Unary arithmetic operators
- ===========================================================================
- */
- #ifndef BIG_INT_UNARY_ARITHMETIC_OPERATORS_HPP
- #define BIG_INT_UNARY_ARITHMETIC_OPERATORS_HPP
- /*
- +BigInt
- -------
- Returns the value of a BigInt.
- NOTE: This function does not return the absolute value. To get the absolute
- value of a BigInt, use the `abs` function.
- */
- BigInt BigInt::operator+() const {
- return *this;
- }
- /*
- -BigInt
- -------
- Returns the negative of a BigInt.
- */
- BigInt BigInt::operator-() const {
- BigInt temp;
- temp.value = value;
- if (value != "0") {
- if (sign == '+')
- temp.sign = '-';
- else
- temp.sign = '+';
- }
- return temp;
- }
- #endif // BIG_INT_UNARY_ARITHMETIC_OPERATORS_HPP
- /*
- ===========================================================================
- Relational operators
- ===========================================================================
- All operators depend on the '<' and/or '==' operator(s).
- */
- #ifndef BIG_INT_RELATIONAL_OPERATORS_HPP
- #define BIG_INT_RELATIONAL_OPERATORS_HPP
- /*
- BigInt == BigInt
- ----------------
- */
- bool BigInt::operator==(const BigInt& num) const {
- return (sign == num.sign) and (value == num.value);
- }
- /*
- BigInt != BigInt
- ----------------
- */
- bool BigInt::operator!=(const BigInt& num) const {
- return !(*this == num);
- }
- /*
- BigInt < BigInt
- ---------------
- */
- bool BigInt::operator<(const BigInt& num) const {
- if (sign == num.sign) {
- if (sign == '+') {
- if (value.length() == num.value.length())
- return value < num.value;
- else
- return value.length() < num.value.length();
- }
- else
- return -(*this) > -num;
- }
- else
- return sign == '-';
- }
- /*
- BigInt > BigInt
- ---------------
- */
- bool BigInt::operator>(const BigInt& num) const {
- return !((*this < num) or (*this == num));
- }
- /*
- BigInt <= BigInt
- ----------------
- */
- bool BigInt::operator<=(const BigInt& num) const {
- return (*this < num) or (*this == num);
- }
- /*
- BigInt >= BigInt
- ----------------
- */
- bool BigInt::operator>=(const BigInt& num) const {
- return !(*this < num);
- }
- /*
- BigInt == Integer
- -----------------
- */
- bool BigInt::operator==(const long long& num) const {
- return *this == BigInt(num);
- }
- /*
- Integer == BigInt
- -----------------
- */
- bool operator==(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) == rhs;
- }
- /*
- BigInt != Integer
- -----------------
- */
- bool BigInt::operator!=(const long long& num) const {
- return !(*this == BigInt(num));
- }
- /*
- Integer != BigInt
- -----------------
- */
- bool operator!=(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) != rhs;
- }
- /*
- BigInt < Integer
- ----------------
- */
- bool BigInt::operator<(const long long& num) const {
- return *this < BigInt(num);
- }
- /*
- Integer < BigInt
- ----------------
- */
- bool operator<(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) < rhs;
- }
- /*
- BigInt > Integer
- ----------------
- */
- bool BigInt::operator>(const long long& num) const {
- return *this > BigInt(num);
- }
- /*
- Integer > BigInt
- ----------------
- */
- bool operator>(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) > rhs;
- }
- /*
- BigInt <= Integer
- -----------------
- */
- bool BigInt::operator<=(const long long& num) const {
- return !(*this > BigInt(num));
- }
- /*
- Integer <= BigInt
- -----------------
- */
- bool operator<=(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) <= rhs;
- }
- /*
- BigInt >= Integer
- -----------------
- */
- bool BigInt::operator>=(const long long& num) const {
- return !(*this < BigInt(num));
- }
- /*
- Integer >= BigInt
- -----------------
- */
- bool operator>=(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) >= rhs;
- }
- /*
- BigInt == String
- ----------------
- */
- bool BigInt::operator==(const std::string& num) const {
- return *this == BigInt(num);
- }
- /*
- String == BigInt
- ----------------
- */
- bool operator==(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) == rhs;
- }
- /*
- BigInt != String
- ----------------
- */
- bool BigInt::operator!=(const std::string& num) const {
- return !(*this == BigInt(num));
- }
- /*
- String != BigInt
- ----------------
- */
- bool operator!=(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) != rhs;
- }
- /*
- BigInt < String
- ---------------
- */
- bool BigInt::operator<(const std::string& num) const {
- return *this < BigInt(num);
- }
- /*
- String < BigInt
- ---------------
- */
- bool operator<(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) < rhs;
- }
- /*
- BigInt > String
- ---------------
- */
- bool BigInt::operator>(const std::string& num) const {
- return *this > BigInt(num);
- }
- /*
- String > BigInt
- ---------------
- */
- bool operator>(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) > rhs;
- }
- /*
- BigInt <= String
- ----------------
- */
- bool BigInt::operator<=(const std::string& num) const {
- return !(*this > BigInt(num));
- }
- /*
- String <= BigInt
- ----------------
- */
- bool operator<=(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) <= rhs;
- }
- /*
- BigInt >= String
- ----------------
- */
- bool BigInt::operator>=(const std::string& num) const {
- return !(*this < BigInt(num));
- }
- /*
- String >= BigInt
- ----------------
- */
- bool operator>=(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) >= rhs;
- }
- #endif // BIG_INT_RELATIONAL_OPERATORS_HPP
- /*
- ===========================================================================
- Math functions for BigInt
- ===========================================================================
- */
- #ifndef BIG_INT_MATH_FUNCTIONS_HPP
- #define BIG_INT_MATH_FUNCTIONS_HPP
- #include <string>
- /*
- abs
- ---
- Returns the absolute value of a BigInt.
- */
- BigInt abs(const BigInt& num) {
- return num < 0 ? -num : num;
- }
- /*
- big_pow10
- ---------
- Returns a BigInt equal to 10^exp.
- NOTE: exponent should be a non-negative integer.
- */
- BigInt big_pow10(size_t exp) {
- return BigInt("1" + std::string(exp, '0'));
- }
- /*
- pow (BigInt)
- ------------
- Returns a BigInt equal to base^exp.
- */
- BigInt pow(const BigInt& base, unsigned int exp) {
- if (exp < 0) {
- if (base == 0)
- throw std::logic_error("Cannot divide by zero");
- return abs(base) == 1 ? base : 0;
- }
- if (exp == 0) {
- if (base == 0)
- throw std::logic_error("Zero cannot be raised to zero");
- return 1;
- }
- BigInt result = base, result_odd = 1;
- while (exp > 1) {
- if (exp % 2)
- result_odd *= result;
- result *= result;
- exp /= 2;
- }
- return result * result_odd;
- }
- BigInt modpow(const BigInt& base, const BigInt& exp, const BigInt& mod) {
- if (exp < 0) {
- if (base == 0)
- throw std::logic_error("Cannot divide by zero");
- return abs(base) == 1 ? base : 0;
- }
- if (exp == 0) {
- if (base == 0)
- throw std::logic_error("Zero cannot be raised to zero");
- return 1;
- }
- BigInt result = base, result_odd = 1;
- while (exp > 1) {
- if (exp % 2 == 0)
- result_odd *= result;
- result *= result;
- exp /= 2;
- }
- return result * result_odd;
- }
- /*
- pow (Integer)
- -------------
- Returns a BigInt equal to base^exp.
- */
- BigInt pow(const long long& base, int exp) {
- return pow(BigInt(base), exp);
- }
- /*
- pow (String)
- ------------
- Returns a BigInt equal to base^exp.
- */
- BigInt pow(const std::string& base, int exp) {
- return pow(BigInt(base), exp);
- }
- /*
- sqrt
- ----
- Returns the positive integer square root of a BigInt using Newton's method.
- NOTE: the input must be non-negative.
- */
- BigInt sqrt(const BigInt& num) {
- if (num < 0)
- throw std::invalid_argument("Cannot compute square root of a negative integer");
- // Optimisations for small inputs:
- if (num == 0)
- return 0;
- else if (num < 4)
- return 1;
- else if (num < 9)
- return 2;
- else if (num < 16)
- return 3;
- BigInt sqrt_prev = 0;
- // The value for `sqrt_current` is chosen close to that of the actual
- // square root.
- // Since a number's square root has at least one less than half as many
- // digits as the number,
- // sqrt_current = 10^(half_the_digits_in_num - 1)
- BigInt sqrt_current = big_pow10(num.to_string().size() / 2 - 1);
- while (abs(sqrt_current - sqrt_prev) > 1) {
- sqrt_prev = sqrt_current;
- sqrt_current = (num / sqrt_prev + sqrt_prev) / 2;
- }
- return sqrt_current;
- }
- #endif // BIG_INT_MATH_FUNCTIONS_HPP
- /*
- ===========================================================================
- Binary arithmetic operators
- ===========================================================================
- */
- #ifndef BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
- #define BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
- #include <climits>
- #include <cmath>
- #include <string>
- const long long FLOOR_SQRT_LLONG_MAX = 3037000499;
- /*
- BigInt + BigInt
- ---------------
- The operand on the RHS of the addition is `num`.
- */
- BigInt BigInt::operator+(const BigInt& num) const {
- // if the operands are of opposite signs, perform subtraction
- if (this->sign == '+' and num.sign == '-') {
- BigInt rhs = num;
- rhs.sign = '+';
- return *this - rhs;
- }
- else if (this->sign == '-' and num.sign == '+') {
- BigInt lhs = *this;
- lhs.sign = '+';
- return -(lhs - num);
- }
- // identify the numbers as `larger` and `smaller`
- std::string larger, smaller;
- std::tie(larger, smaller) = get_larger_and_smaller(this->value, num.value);
- BigInt result; // the resultant sum
- result.value = ""; // the value is cleared as the digits will be appended
- short carry = 0, sum;
- // add the two values
- for (long i = larger.size() - 1; i >= 0; i--) {
- sum = larger[i] - '0' + smaller[i] - '0' + carry;
- result.value = std::to_string(sum % 10) + result.value;
- carry = sum / (short) 10;
- }
- if (carry)
- result.value = std::to_string(carry) + result.value;
- // if the operands are negative, the result is negative
- if (this->sign == '-' and result.value != "0")
- result.sign = '-';
- return result;
- }
- /*
- BigInt - BigInt
- ---------------
- The operand on the RHS of the subtraction is `num`.
- */
- BigInt BigInt::operator-(const BigInt& num) const {
- // if the operands are of opposite signs, perform addition
- if (this->sign == '+' and num.sign == '-') {
- BigInt rhs = num;
- rhs.sign = '+';
- return *this + rhs;
- }
- else if (this->sign == '-' and num.sign == '+') {
- BigInt lhs = *this;
- lhs.sign = '+';
- return -(lhs + num);
- }
- BigInt result; // the resultant difference
- // identify the numbers as `larger` and `smaller`
- std::string larger, smaller;
- if (abs(*this) > abs(num)) {
- larger = this->value;
- smaller = num.value;
- if (this->sign == '-') // -larger - -smaller = -result
- result.sign = '-';
- }
- else {
- larger = num.value;
- smaller = this->value;
- if (num.sign == '+') // smaller - larger = -result
- result.sign = '-';
- }
- // pad the smaller number with zeroes
- add_leading_zeroes(smaller, larger.size() - smaller.size());
- result.value = ""; // the value is cleared as the digits will be appended
- short difference;
- long i, j;
- // subtract the two values
- for (i = larger.size() - 1; i >= 0; i--) {
- difference = larger[i] - smaller[i];
- if (difference < 0) {
- for (j = i - 1; j >= 0; j--) {
- if (larger[j] != '0') {
- larger[j]--; // borrow from the j-th digit
- break;
- }
- }
- j++;
- while (j != i) {
- larger[j] = '9'; // add the borrow and take away 1
- j++;
- }
- difference += 10; // add the borrow
- }
- result.value = std::to_string(difference) + result.value;
- }
- strip_leading_zeroes(result.value);
- // if the result is 0, set its sign as +
- if (result.value == "0")
- result.sign = '+';
- return result;
- }
- /*
- BigInt * BigInt
- ---------------
- Computes the product of two BigInts using Karatsuba's algorithm.
- The operand on the RHS of the product is `num`.
- */
- BigInt BigInt::operator*(const BigInt& num) const {
- if (*this == 0 or num == 0)
- return BigInt(0);
- if (*this == 1)
- return num;
- if (num == 1)
- return *this;
- BigInt product;
- if (abs(*this) <= FLOOR_SQRT_LLONG_MAX and abs(num) <= FLOOR_SQRT_LLONG_MAX)
- product = std::stoll(this->value) * std::stoll(num.value);
- else {
- // identify the numbers as `larger` and `smaller`
- std::string larger, smaller;
- std::tie(larger, smaller) = get_larger_and_smaller(this->value, num.value);
- size_t half_length = larger.size() / 2;
- auto half_length_ceil = (size_t) ceil(larger.size() / 2.0);
- BigInt num1_high, num1_low;
- num1_high = larger.substr(0, half_length);
- num1_low = larger.substr(half_length);
- BigInt num2_high, num2_low;
- num2_high = smaller.substr(0, half_length);
- num2_low = smaller.substr(half_length);
- strip_leading_zeroes(num1_high.value);
- strip_leading_zeroes(num1_low.value);
- strip_leading_zeroes(num2_high.value);
- strip_leading_zeroes(num2_low.value);
- BigInt prod_high, prod_mid, prod_low;
- prod_high = num1_high * num2_high;
- prod_low = num1_low * num2_low;
- prod_mid = (num1_high + num1_low) * (num2_high + num2_low)
- - prod_high - prod_low;
- add_trailing_zeroes(prod_high.value, 2 * half_length_ceil);
- add_trailing_zeroes(prod_mid.value, half_length_ceil);
- strip_leading_zeroes(prod_high.value);
- strip_leading_zeroes(prod_mid.value);
- strip_leading_zeroes(prod_low.value);
- product = prod_high + prod_mid + prod_low;
- }
- strip_leading_zeroes(product.value);
- if (this->sign == num.sign)
- product.sign = '+';
- else
- product.sign = '-';
- return product;
- }
- /*
- divide
- ------
- Helper function that returns the quotient and remainder on dividing the
- dividend by the divisor, when the divisor is 1 to 10 times the dividend.
- */
- std::tuple<BigInt, BigInt> divide(const BigInt& dividend, const BigInt& divisor) {
- BigInt quotient, remainder, temp;
- temp = divisor;
- quotient = 1;
- while (temp < dividend) {
- quotient++;
- temp += divisor;
- }
- if (temp > dividend) {
- quotient--;
- remainder = dividend - (temp - divisor);
- }
- return std::make_tuple(quotient, remainder);
- }
- /*
- BigInt / BigInt
- ---------------
- Computes the quotient of two BigInts using the long-division method.
- The operand on the RHS of the division (the divisor) is `num`.
- */
- BigInt BigInt::operator/(const BigInt& num) const {
- BigInt abs_dividend = abs(*this);
- BigInt abs_divisor = abs(num);
- if (num == 0)
- throw std::logic_error("Attempted division by zero");
- if (abs_dividend < abs_divisor)
- return BigInt(0);
- if (num == 1)
- return *this;
- if (num == -1)
- return -(*this);
- BigInt quotient;
- if (abs_dividend <= LLONG_MAX and abs_divisor <= LLONG_MAX)
- quotient = std::stoll(abs_dividend.value) / std::stoll(abs_divisor.value);
- else if (abs_dividend == abs_divisor)
- quotient = 1;
- else {
- quotient.value = ""; // the value is cleared as digits will be appended
- BigInt chunk, chunk_quotient, chunk_remainder;
- size_t chunk_index = 0;
- chunk_remainder.value = abs_dividend.value.substr(chunk_index, abs_divisor.value.size() - 1);
- chunk_index = abs_divisor.value.size() - 1;
- while (chunk_index < abs_dividend.value.size()) {
- chunk.value = chunk_remainder.value.append(1, abs_dividend.value[chunk_index]);
- chunk_index++;
- while (chunk < abs_divisor) {
- quotient.value += "0";
- if (chunk_index < abs_dividend.value.size()) {
- chunk.value.append(1, abs_dividend.value[chunk_index]);
- chunk_index++;
- }
- else
- break;
- }
- if (chunk == abs_divisor) {
- quotient.value += "1";
- chunk_remainder = 0;
- }
- else if (chunk > abs_divisor) {
- strip_leading_zeroes(chunk.value);
- std::tie(chunk_quotient, chunk_remainder) = divide(chunk, abs_divisor);
- quotient.value += chunk_quotient.value;
- }
- }
- }
- strip_leading_zeroes(quotient.value);
- if (this->sign == num.sign)
- quotient.sign = '+';
- else
- quotient.sign = '-';
- return quotient;
- }
- /*
- BigInt % BigInt
- ---------------
- Computes the modulo (remainder on division) of two BigInts.
- The operand on the RHS of the modulo (the divisor) is `num`.
- */
- BigInt BigInt::operator%(const BigInt& num) const {
- BigInt abs_dividend = abs(*this);
- BigInt abs_divisor = abs(num);
- if (abs_divisor == 0)
- throw std::logic_error("Attempted division by zero");
- if (abs_divisor == 1 or abs_divisor == abs_dividend)
- return BigInt(0);
- BigInt remainder;
- if (abs_dividend <= LLONG_MAX and abs_divisor <= LLONG_MAX)
- remainder = std::stoll(abs_dividend.value) % std::stoll(abs_divisor.value);
- else if (abs_dividend < abs_divisor)
- remainder = abs_dividend;
- else {
- BigInt quotient = abs_dividend / abs_divisor;
- remainder = abs_dividend - quotient * abs_divisor;
- }
- strip_leading_zeroes(remainder.value);
- // remainder has the same sign as that of the dividend
- remainder.sign = this->sign;
- if (remainder.value == "0") // except if its zero
- remainder.sign = '+';
- return remainder;
- }
- /*
- BigInt + Integer
- ----------------
- */
- BigInt BigInt::operator+(const long long& num) const {
- return *this + BigInt(num);
- }
- /*
- Integer + BigInt
- ----------------
- */
- BigInt operator+(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) + rhs;
- }
- /*
- BigInt - Integer
- ----------------
- */
- BigInt BigInt::operator-(const long long& num) const {
- return *this - BigInt(num);
- }
- /*
- Integer - BigInt
- ----------------
- */
- BigInt operator-(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) - rhs;
- }
- /*
- BigInt * Integer
- ----------------
- */
- BigInt BigInt::operator*(const long long& num) const {
- return *this * BigInt(num);
- }
- /*
- Integer * BigInt
- ----------------
- */
- BigInt operator*(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) * rhs;
- }
- /*
- BigInt / Integer
- ----------------
- */
- BigInt BigInt::operator/(const long long& num) const {
- return *this / BigInt(num);
- }
- /*
- Integer / BigInt
- ----------------
- */
- BigInt operator/(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) / rhs;
- }
- /*
- BigInt % Integer
- ----------------
- */
- BigInt BigInt::operator%(const long long& num) const {
- return *this % BigInt(num);
- }
- /*
- Integer % BigInt
- ----------------
- */
- BigInt operator%(const long long& lhs, const BigInt& rhs) {
- return BigInt(lhs) % rhs;
- }
- /*
- BigInt + String
- ---------------
- */
- BigInt BigInt::operator+(const std::string& num) const {
- return *this + BigInt(num);
- }
- /*
- String + BigInt
- ---------------
- */
- BigInt operator+(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) + rhs;
- }
- /*
- BigInt - String
- ---------------
- */
- BigInt BigInt::operator-(const std::string& num) const {
- return *this - BigInt(num);
- }
- /*
- String - BigInt
- ---------------
- */
- BigInt operator-(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) - rhs;
- }
- /*
- BigInt * String
- ---------------
- */
- BigInt BigInt::operator*(const std::string& num) const {
- return *this * BigInt(num);
- }
- /*
- String * BigInt
- ---------------
- */
- BigInt operator*(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) * rhs;
- }
- /*
- BigInt / String
- ---------------
- */
- BigInt BigInt::operator/(const std::string& num) const {
- return *this / BigInt(num);
- }
- /*
- String / BigInt
- ---------------
- */
- BigInt operator/(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) / rhs;
- }
- /*
- BigInt % String
- ---------------
- */
- BigInt BigInt::operator%(const std::string& num) const {
- return *this % BigInt(num);
- }
- /*
- String % BigInt
- ---------------
- */
- BigInt operator%(const std::string& lhs, const BigInt& rhs) {
- return BigInt(lhs) % rhs;
- }
- #endif // BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
- /*
- ===========================================================================
- Arithmetic-assignment operators
- ===========================================================================
- */
- #ifndef BIG_INT_ARITHMETIC_ASSIGNMENT_OPERATORS_HPP
- #define BIG_INT_ARITHMETIC_ASSIGNMENT_OPERATORS_HPP
- /*
- BigInt += BigInt
- ----------------
- */
- BigInt& BigInt::operator+=(const BigInt& num) {
- *this = *this + num;
- return *this;
- }
- /*
- BigInt -= BigInt
- ----------------
- */
- BigInt& BigInt::operator-=(const BigInt& num) {
- *this = *this - num;
- return *this;
- }
- /*
- BigInt *= BigInt
- ----------------
- */
- BigInt& BigInt::operator*=(const BigInt& num) {
- *this = *this * num;
- return *this;
- }
- /*
- BigInt /= BigInt
- ----------------
- */
- BigInt& BigInt::operator/=(const BigInt& num) {
- *this = *this / num;
- return *this;
- }
- /*
- BigInt %= BigInt
- ----------------
- */
- BigInt& BigInt::operator%=(const BigInt& num) {
- *this = *this % num;
- return *this;
- }
- /*
- BigInt += Integer
- -----------------
- */
- BigInt& BigInt::operator+=(const long long& num) {
- *this = *this + BigInt(num);
- return *this;
- }
- /*
- BigInt -= Integer
- -----------------
- */
- BigInt& BigInt::operator-=(const long long& num) {
- *this = *this - BigInt(num);
- return *this;
- }
- /*
- BigInt *= Integer
- -----------------
- */
- BigInt& BigInt::operator*=(const long long& num) {
- *this = *this * BigInt(num);
- return *this;
- }
- /*
- BigInt /= Integer
- -----------------
- */
- BigInt& BigInt::operator/=(const long long& num) {
- *this = *this / BigInt(num);
- return *this;
- }
- /*
- BigInt %= Integer
- -----------------
- */
- BigInt& BigInt::operator%=(const long long& num) {
- *this = *this % BigInt(num);
- return *this;
- }
- /*
- BigInt += String
- ----------------
- */
- BigInt& BigInt::operator+=(const std::string& num) {
- *this = *this + BigInt(num);
- return *this;
- }
- /*
- BigInt -= String
- ----------------
- */
- BigInt& BigInt::operator-=(const std::string& num) {
- *this = *this - BigInt(num);
- return *this;
- }
- /*
- BigInt *= String
- ----------------
- */
- BigInt& BigInt::operator*=(const std::string& num) {
- *this = *this * BigInt(num);
- return *this;
- }
- /*
- BigInt /= String
- ----------------
- */
- BigInt& BigInt::operator/=(const std::string& num) {
- *this = *this / BigInt(num);
- return *this;
- }
- /*
- BigInt %= String
- ----------------
- */
- BigInt& BigInt::operator%=(const std::string& num) {
- *this = *this % BigInt(num);
- return *this;
- }
- #endif // BIG_INT_ARITHMETIC_ASSIGNMENT_OPERATORS_HPP
- /*
- ===========================================================================
- Increment and decrement operators
- ===========================================================================
- */
- #ifndef BIG_INT_INCREMENT_DECREMENT_OPERATORS_HPP
- #define BIG_INT_INCREMENT_DECREMENT_OPERATORS_HPP
- /*
- Pre-increment
- -------------
- ++BigInt
- */
- BigInt& BigInt::operator++() {
- *this += 1;
- return *this;
- }
- /*
- Pre-decrement
- -------------
- --BigInt
- */
- BigInt& BigInt::operator--() {
- *this -= 1;
- return *this;
- }
- /*
- Post-increment
- --------------
- BigInt++
- */
- BigInt BigInt::operator++(int) {
- BigInt temp = *this;
- *this += 1;
- return temp;
- }
- /*
- Post-decrement
- --------------
- BigInt--
- */
- BigInt BigInt::operator--(int) {
- BigInt temp = *this;
- *this -= 1;
- return temp;
- }
- #endif // BIG_INT_INCREMENT_DECREMENT_OPERATORS_HPP
- /*
- ===========================================================================
- I/O stream operators
- ===========================================================================
- */
- #ifndef BIG_INT_IO_STREAM_OPERATORS_HPP
- #define BIG_INT_IO_STREAM_OPERATORS_HPP
- /*
- BigInt from input stream
- ------------------------
- */
- std::istream& operator>>(std::istream& in, BigInt& num) {
- std::string input;
- in >> input;
- num = BigInt(input); // remove sign from value and set sign, if exists
- return in;
- }
- /*
- BigInt to output stream
- -----------------------
- */
- std::ostream& operator<<(std::ostream& out, const BigInt& num) {
- if (num.sign == '-')
- out << num.sign;
- out << num.value;
- return out;
- }
- #endif // BIG_INT_IO_STREAM_OPERATORS_HPP
|