ja / en
Overview

素因数分解

2024/07/29
1 min read

Purpose

Computes the prime factorization of a given natural number.

Time Complexity

O(nlnn)O\left(\dfrac{n}{\ln n}\right)

Usage

For example, primeFactorize(12) returns {(2,2),(3,1)}. This means 2231=122^2 \cdot 3^1 = 12.

Implementation

vector<pair<int, int>> primeFactorize(int n) {
vector<pair<int, int>> res;
for (int a = 2; a * a <= n; ++a) {
if (n % a == 0) {
res.push_back({a, 0});
while (n % a == 0)
++res.back().second, n /= a;
}
}
if (n != 1)
res.push_back({n, 1});
return res;
}

Verify

//TODO