Skip to content

Commit 28c3af7

Browse files
committed
docs: Add documentation for Pollard's Rho algorithm and factorization functions
1 parent 470cde3 commit 28c3af7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

weilycoder/number-theory/factorize.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef WEILYCODER_NUMBER_THEORY_FACTORIZE_HPP
22
#define WEILYCODER_NUMBER_THEORY_FACTORIZE_HPP
33

4+
/**
5+
* @file factorize.hpp
6+
* @brief Functions for factorizing numbers using Pollard's Rho algorithm
7+
*/
8+
49
#include "mod_utility.hpp"
510
#include "prime.hpp"
611
#include <algorithm>
@@ -12,6 +17,13 @@
1217
#include <vector>
1318

1419
namespace weilycoder {
20+
/**
21+
* @brief Pollard's Rho algorithm to find a non-trivial factor of x
22+
* @tparam bit32 Whether to use 32-bit modular multiplication
23+
* @param x The number to factorize
24+
* @param c The constant in the polynomial x^2 + c
25+
* @return A non-trivial factor of x
26+
*/
1527
template <bool bit32 = false> constexpr uint64_t pollard_rho(uint64_t x, uint64_t c) {
1628
if (x % 2 == 0)
1729
return 2;
@@ -38,6 +50,12 @@ template <bool bit32 = false> constexpr uint64_t pollard_rho(uint64_t x, uint64_
3850
return x;
3951
}
4052

53+
/**
54+
* @brief Pollard's Rho algorithm to find a non-trivial factor of x
55+
* @tparam bit32 Whether to use 32-bit modular multiplication
56+
* @param x The number to factorize
57+
* @return A non-trivial factor of x
58+
*/
4159
template <bool bit32 = false> uint64_t pollard_rho(uint64_t x) {
4260
static std::minstd_rand rng{};
4361
if (x % 2 == 0)
@@ -46,6 +64,12 @@ template <bool bit32 = false> uint64_t pollard_rho(uint64_t x) {
4664
return pollard_rho<bit32>(x, c);
4765
}
4866

67+
/**
68+
* @brief Factorize a number into its prime factors
69+
* @tparam bit32 Whether to use 32-bit modular multiplication
70+
* @param x The number to factorize
71+
* @return A vector of prime factors of x
72+
*/
4973
template <bool bit32 = false> std::vector<uint64_t> factorize(uint64_t x) {
5074
std::vector<uint64_t> factors;
5175
std::vector<std::pair<uint64_t, size_t>> stk;
@@ -73,6 +97,13 @@ template <bool bit32 = false> std::vector<uint64_t> factorize(uint64_t x) {
7397
return factors;
7498
}
7599

100+
/**
101+
* @brief Factorize a number into its prime factors with fixed-size array
102+
* @tparam N The size of the output array
103+
* @tparam bit32 Whether to use 32-bit modular multiplication
104+
* @param x The number to factorize
105+
* @return An array of prime factors of x
106+
*/
76107
template <size_t N = 64, bool bit32 = false>
77108
constexpr std::array<uint64_t, N> factorize_fixed(uint64_t x) {
78109
size_t factor_idx = 0, stk_idx = 0;

0 commit comments

Comments
 (0)