Commit 34bf146b authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

add IntegerP.h and IntegerPP.h

parent cb4c9756
Loading
Loading
Loading
Loading

IntegerP.h

0 → 100644
+127 −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>
#include <string>

/**
 * 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)
     */
    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 84
     * @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

IntegerPP.h

0 → 100644
+51 −0
Original line number Diff line number Diff line
//
// Created by Ari on 1/31/26.
//

#ifndef HW1_INTEGERPLUS_H
#define HW1_INTEGERPLUS_H
#include "IntegerP.h"
#include "BigNum.h"

class IntegerPP: public IntegerP {
  public:
    /**
     * All modeled after {@link IntegerP}.
     */
    IntegerPP();
    IntegerPP(unsigned long n);
    IntegerPP(std::initializer_list<int> pBasis);
    IntegerPP(const IntegerP& other);


    /**
     * Constructs an IntegerPlus from a BigNum
     * @param num The BigNum from which to construct the IntegerPlus.
     */
    explicit IntegerPP(BigNum num);

    /**
     * Adds this {@link IntegerPP} by {@code addor} and returns the sum.
     * @param addor The number to add to this object.
     * @return The sum of this object and {@code addor}.
     * @example (IntegerPP(10) + IntegerPP(5) == IntegerPP(15))
     * @example (IntegerPP({1,1}) * IntegerPP({1,0,0,1}) == IntegerPP({2,0,1}))
     *          (2*3) + (2*7) = (2^2 * 5)
     *              6 +    14 = 20
     */
    IntegerPP operator+(const IntegerPP &addor) const;

    /**
     * Assigns the {@link rhs} to this object.
     * @param rhs The element to assign.
     * @return this object.
     */
    IntegerPP& operator=(const IntegerPP& rhs);

    /**
     * Converts this IntegerPP into a BigNum.
     */
    explicit operator BigNum() const;
};

#endif //HW1_INTEGERPLUS_H
 No newline at end of file