Loading IntegerP.h 0 → 100644 +128 −0 Original line number Diff line number Diff line // // Created by Ari Trachtenberg on 1/23/26. // #ifndef INITEGERP_H #define INITEGERP_H #include <vector> /** * Represents a multi-precision, positive Integer, in a manner suitable * for efficiently multiplying and dividing large powers of small integers, * such as 6^1000 and 10^512. */ class IntegerP { public: // CONSTRUCTORS /** * Constructs the Integer 1. */ IntegerP(); /** * Constructs an {@link IntegerP} representation of num. * @requires num>0 */ IntegerP(unsigned long lnum); /** * Constructs a representation of an integer in the prime basis. * Specifically, the number produced is: * product over all i of (prime(i)^basis[i]), * where prime(i) is the i-th prime number (2 being the 0-th prime), and * basis[i] is the i-th element of parameter {@code pBasis}. * @param pBasis A list representing the powers of primes generating this {@link IntegerP}. * @requires pBasis[i]>=0 for all i * @example IntegerP ({1}) represents the number 2 = 2^1 * @example IntegerP ({1,1}) represents the number 6 = 2^1 * 3^1 * @example IntegerP ({1,0,0,1}) represents the number 14 = 2^1 * 3^0 * 5^0 * 7^1 * @example IntegerP ({0,2,0,1,1}) represents the number 693 = 3^2 * 7 * 11 */ IntegerP(std::initializer_list<int> pBasis); /** * Static factory method based on {@link #IntegerP(unsigned long)}. * @example IntegerP::valueOf(3) returns an IntegerP representing 3 = 3^1. */ static IntegerP valueOf(const unsigned long lnum) { return IntegerP(lnum); } /** * Static factory method based on {@link #IntegerP(std::initializer_list<int>)}. * @example IntegerP::valueOf({1}) represents the number 2 = 2^1 * @example IntegerP::valueOf({1,1}) represents the number 6 = 2^1 * 3^1 * @example IntegerP::valueOf({1,0,0,1}) represents the number 14 = 2^1 * 3^0 * 5^0 * 7^1 * @example IntegerP::valueOf({0,2,0,1,1}) represents the number 693 = 3^2 * 7 * 11 */ static IntegerP valueOf(const std::initializer_list<int> pBasis) { return IntegerP(pBasis); } // OPERATORS /** * Multiplies this {@link IntegerP} by {@code multiplicand} and returns the product. * @param multiplicand The number to multiply by this object. * @return The product of this object and {@code multiplicand}. * @example IntegerP::valueOf(10) * Integer::valueOf(5) = Integer::valueOf(50) * @example IntegerP::valueOf({1,1}) * IntegerP::valueOf({1,0,0,1}) = IntegerP::valueOf({2,1,0,1}) * (2*3) * (2*7) = (2^2 * 3 * 7) * 6 * 14 = 84 */ IntegerP operator*(const IntegerP &multiplicand) const; /** * Divides this {@link IntegerP} by {@code divisor} and returns the quotient. * @param divisor The number into which to divide this object. * @return The division of this object and {@code divisor}. If this object is not * evenly divisible by {@code divisor}, the result is not specified. * @example IntegerP::valueOf(14) / IntegerP::valueOf(2) = IntegerP::valueOf(7) * @example IntegerP::valueOf(14) / IntegerP::valueOf(3) throws indivisible_exception * 14 / 3 is not an integer. */ IntegerP operator/(const IntegerP &divisor) const; /** * @return true iff {@code other} is *identical* to this Rational number * @example IntegerP::valueOf(14)==Integer::valueOf({1,0,0,1}) returns true */ bool operator==(const IntegerP &other) const; // INFORMATIONAL /** * @return A truncation of this object to a {@code long}, or * LONG_MAX if the object represents a number that is too big * to fit in a {@code long}. * @example IntegerP::valueOf(14).trunc() returns 14 * @example IntegerP::valueOf({2,1,0,1}).trunc() returns 82 * @example IntegerP::valueOf({20,3,5,10}).trunc() retunrs LONG_MAX (=9223372036854775807) */ long trunc() const; /** * @param divisor The divisor to check for divisibility * @return true iff this object is evenly divisible by {@code divisor}. * @example IntegerP::valueOf(14).divisibleBy(IntegerP::valueOf(2)) returns true * @example IntegerP::valueOf(14).divisibleBy(IntegerP::valueOf(3)) returns false */ bool divisibleBy(const IntegerP &divisor) const; /** * @return A human-readable representation of this object. * @example IntegerP::valueOf(2928).toString() may return "2^4 * 3 * 61" */ std::string toString() const; /** * Safe getter method for the {@link #_primePowers} field. * @return A read-only version of {@link #_primePowers}. */ const std::vector<int>& primePowers() const noexcept { return _primePowers; } protected: /** * the i-th entry contains the power of i-th prime in the product that generates this object. */ std::vector<int> _primePowers; }; #endif //IntegerP_H RationalP.h 0 → 100644 +88 −0 Original line number Diff line number Diff line // // Created by Ari Trachtenberg on 1/23/26. // #ifndef RATIONALP_H #define RATIONALP_H #include "IntegerP.h" /** * Represents a multi-precision rational number, in a manner suitable * for manipulating large and small powers, such as 2^1000 or (3/2)^-1000. */ class RationalP : public IntegerP { public: // CONSTRUCTORS /** * Constructs the Rational 1. */ RationalP(); /** * Constructs a RationalP representing the IntegerP {@code intp}. */ explicit RationalP(const IntegerP &intp); /** * Produces a representation of num / denom. * @requires denom != 0 */ RationalP(const IntegerP &num, const IntegerP &denom); /** * {@inheritDoc} * @param pBasis Components may be negative, zero, or positive */ RationalP(const std::initializer_list<int> &pBasis); /** * @param num The numerator * @param denom The denominator * @requires denom != 0 * @return A RationalP representing num / denom. */ static RationalP valueOf(const IntegerP &num, const IntegerP &denom); /** * {@inheritDoc} * @param pBasis Components may be negative, zero, or positive */ static RationalP valueOf(std::initializer_list<int> pBasis); // OPERATORS /** * {@inheritDoc} */ RationalP operator*(const RationalP &multiplicand) const; /** * {@inheritDoc} */ RationalP operator/(const RationalP &divisor) const; /** * {@inheritDoc} */ bool operator==(const RationalP &other) const; // INFORMATIONAL /** * @return An approximation of this Rational number, expressed as a long double. */ long double trunc() const; /** * {@inheritDoc} */ bool divisibleBy(const RationalP &other) const; /** * {@inheritDoc} */ std::string toString() const; }; #endif //RATIONALP_H Loading
IntegerP.h 0 → 100644 +128 −0 Original line number Diff line number Diff line // // Created by Ari Trachtenberg on 1/23/26. // #ifndef INITEGERP_H #define INITEGERP_H #include <vector> /** * Represents a multi-precision, positive Integer, in a manner suitable * for efficiently multiplying and dividing large powers of small integers, * such as 6^1000 and 10^512. */ class IntegerP { public: // CONSTRUCTORS /** * Constructs the Integer 1. */ IntegerP(); /** * Constructs an {@link IntegerP} representation of num. * @requires num>0 */ IntegerP(unsigned long lnum); /** * Constructs a representation of an integer in the prime basis. * Specifically, the number produced is: * product over all i of (prime(i)^basis[i]), * where prime(i) is the i-th prime number (2 being the 0-th prime), and * basis[i] is the i-th element of parameter {@code pBasis}. * @param pBasis A list representing the powers of primes generating this {@link IntegerP}. * @requires pBasis[i]>=0 for all i * @example IntegerP ({1}) represents the number 2 = 2^1 * @example IntegerP ({1,1}) represents the number 6 = 2^1 * 3^1 * @example IntegerP ({1,0,0,1}) represents the number 14 = 2^1 * 3^0 * 5^0 * 7^1 * @example IntegerP ({0,2,0,1,1}) represents the number 693 = 3^2 * 7 * 11 */ IntegerP(std::initializer_list<int> pBasis); /** * Static factory method based on {@link #IntegerP(unsigned long)}. * @example IntegerP::valueOf(3) returns an IntegerP representing 3 = 3^1. */ static IntegerP valueOf(const unsigned long lnum) { return IntegerP(lnum); } /** * Static factory method based on {@link #IntegerP(std::initializer_list<int>)}. * @example IntegerP::valueOf({1}) represents the number 2 = 2^1 * @example IntegerP::valueOf({1,1}) represents the number 6 = 2^1 * 3^1 * @example IntegerP::valueOf({1,0,0,1}) represents the number 14 = 2^1 * 3^0 * 5^0 * 7^1 * @example IntegerP::valueOf({0,2,0,1,1}) represents the number 693 = 3^2 * 7 * 11 */ static IntegerP valueOf(const std::initializer_list<int> pBasis) { return IntegerP(pBasis); } // OPERATORS /** * Multiplies this {@link IntegerP} by {@code multiplicand} and returns the product. * @param multiplicand The number to multiply by this object. * @return The product of this object and {@code multiplicand}. * @example IntegerP::valueOf(10) * Integer::valueOf(5) = Integer::valueOf(50) * @example IntegerP::valueOf({1,1}) * IntegerP::valueOf({1,0,0,1}) = IntegerP::valueOf({2,1,0,1}) * (2*3) * (2*7) = (2^2 * 3 * 7) * 6 * 14 = 84 */ IntegerP operator*(const IntegerP &multiplicand) const; /** * Divides this {@link IntegerP} by {@code divisor} and returns the quotient. * @param divisor The number into which to divide this object. * @return The division of this object and {@code divisor}. If this object is not * evenly divisible by {@code divisor}, the result is not specified. * @example IntegerP::valueOf(14) / IntegerP::valueOf(2) = IntegerP::valueOf(7) * @example IntegerP::valueOf(14) / IntegerP::valueOf(3) throws indivisible_exception * 14 / 3 is not an integer. */ IntegerP operator/(const IntegerP &divisor) const; /** * @return true iff {@code other} is *identical* to this Rational number * @example IntegerP::valueOf(14)==Integer::valueOf({1,0,0,1}) returns true */ bool operator==(const IntegerP &other) const; // INFORMATIONAL /** * @return A truncation of this object to a {@code long}, or * LONG_MAX if the object represents a number that is too big * to fit in a {@code long}. * @example IntegerP::valueOf(14).trunc() returns 14 * @example IntegerP::valueOf({2,1,0,1}).trunc() returns 82 * @example IntegerP::valueOf({20,3,5,10}).trunc() retunrs LONG_MAX (=9223372036854775807) */ long trunc() const; /** * @param divisor The divisor to check for divisibility * @return true iff this object is evenly divisible by {@code divisor}. * @example IntegerP::valueOf(14).divisibleBy(IntegerP::valueOf(2)) returns true * @example IntegerP::valueOf(14).divisibleBy(IntegerP::valueOf(3)) returns false */ bool divisibleBy(const IntegerP &divisor) const; /** * @return A human-readable representation of this object. * @example IntegerP::valueOf(2928).toString() may return "2^4 * 3 * 61" */ std::string toString() const; /** * Safe getter method for the {@link #_primePowers} field. * @return A read-only version of {@link #_primePowers}. */ const std::vector<int>& primePowers() const noexcept { return _primePowers; } protected: /** * the i-th entry contains the power of i-th prime in the product that generates this object. */ std::vector<int> _primePowers; }; #endif //IntegerP_H
RationalP.h 0 → 100644 +88 −0 Original line number Diff line number Diff line // // Created by Ari Trachtenberg on 1/23/26. // #ifndef RATIONALP_H #define RATIONALP_H #include "IntegerP.h" /** * Represents a multi-precision rational number, in a manner suitable * for manipulating large and small powers, such as 2^1000 or (3/2)^-1000. */ class RationalP : public IntegerP { public: // CONSTRUCTORS /** * Constructs the Rational 1. */ RationalP(); /** * Constructs a RationalP representing the IntegerP {@code intp}. */ explicit RationalP(const IntegerP &intp); /** * Produces a representation of num / denom. * @requires denom != 0 */ RationalP(const IntegerP &num, const IntegerP &denom); /** * {@inheritDoc} * @param pBasis Components may be negative, zero, or positive */ RationalP(const std::initializer_list<int> &pBasis); /** * @param num The numerator * @param denom The denominator * @requires denom != 0 * @return A RationalP representing num / denom. */ static RationalP valueOf(const IntegerP &num, const IntegerP &denom); /** * {@inheritDoc} * @param pBasis Components may be negative, zero, or positive */ static RationalP valueOf(std::initializer_list<int> pBasis); // OPERATORS /** * {@inheritDoc} */ RationalP operator*(const RationalP &multiplicand) const; /** * {@inheritDoc} */ RationalP operator/(const RationalP &divisor) const; /** * {@inheritDoc} */ bool operator==(const RationalP &other) const; // INFORMATIONAL /** * @return An approximation of this Rational number, expressed as a long double. */ long double trunc() const; /** * {@inheritDoc} */ bool divisibleBy(const RationalP &other) const; /** * {@inheritDoc} */ std::string toString() const; }; #endif //RATIONALP_H