// Copyright 2025 Jeisson Hidalgo-Cespedes. ECCI-UCR. CC-BY-4.0 #pragma once #include #include #include #include "common.hpp" typedef int64_t num_t; /** * @brief Find the prime factorization of a number. */ class Factorization { // Copies of objects of this class are allowed DECLARE_RULE4(Factorization, default); private: /// The number to be factorized num_t number = 0; /// The prime factorization as a list of powers. Each power is a pair of /// two consecutive numbers representing (base, exponent) std::vector powers; public: /// Constructor explicit Factorization(const num_t number = 0); /// Destructor ~Factorization() = default; /// Compute the prime factorization of a number /// @param number The number to factorize /// @return A vector of pairs (prime, exponent) representing the factorization void factorize(); /// Print the prime factorization to a file inline friend std::ostream& operator<<(std::ostream& output, const Factorization& factorization) { return factorization.print(output); } /// Print the prime factorization to a file std::ostream& print(std::ostream& output) const; private: /// @brief Divide the number by the divisor as much as times as it could. /// For each successful division, the divisor and the count of times it /// was divided are stored in the powers vector. /// @param copy Copy of the number to be divided /// @param divisor The potential prime used to divide the number /// @return The number after the division finishes num_t divide(num_t copy, const num_t divisor); };