Skip to content

prime factorization by lpf#203

Open
ia7ck wants to merge 4 commits intomasterfrom
prime-factors
Open

prime factorization by lpf#203
ia7ck wants to merge 4 commits intomasterfrom
prime-factors

Conversation

@ia7ck
Copy link
Owner

@ia7ck ia7ck commented Feb 15, 2026

No description provided.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the prime_factorization library to support prime factorization via two strategies (trial division and least-prime-factor precomputation), and adjusts the least_prime_factors crate API to compute factors up to and including n.

Changes:

  • Replaced the old “method on integers” API with a strategy-based PrimeFactorization<T> trait plus TrialDivision and ByLeastPrimeFactors implementations.
  • Added a dependency from prime_factorization to the local least_prime_factors crate.
  • Updated least_prime_factors(n) to return a table covering indices 0..=n (inclusive), with updated docs/tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
libs/prime_factorization/src/lib.rs Introduces strategy-based factorization APIs and adds LPF-backed implementation + tests/docs.
libs/prime_factorization/Cargo.toml Adds a path dependency on least_prime_factors.
libs/least_prime_factors/src/lib.rs Changes LPF sieve to compute results for 2..=n and updates documentation/tests accordingly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// ```
pub fn least_prime_factors(n: usize) -> Vec<usize> {
let mut result = vec![0; n];
let mut result = vec![0; n + 1];
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vec![0; n + 1] can overflow when n == usize::MAX (wrap in release, panic in debug), leading to confusing behavior/panics later. Consider using n.checked_add(1) (and an explicit panic message or early return) to make the failure mode well-defined.

Suggested change
let mut result = vec![0; n + 1];
let size = n.checked_add(1).expect("n is too large to compute least_prime_factors");
let mut result = vec![0; size];

Copilot uses AI. Check for mistakes.
Comment on lines +79 to +83
/// 素因数分解の前計算として [least prime factors](least_prime_factors::least_prime_factors) を求めます。
pub fn new(n: usize) -> Self {
let lpf = least_prime_factors(n);
Self { lpf }
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ByLeastPrimeFactors::new(n) creates a table that only supports factors(x) for x <= n (otherwise it panics). The doc comment for new should explicitly state this precondition (and ideally mention the O(n) memory cost) so callers know what range is supported.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +47 to +48
// p * p > x
if p > x / p {
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In TrialDivision::factors, the loop termination condition uses the original x (if p > x / p) even after y has been reduced by dividing out factors. For inputs like large powers of 2, y quickly becomes 1 but the loop still runs up to sqrt(x), which can be billions of iterations for u64. Use the current remainder (y) in the p*p > ... check (and/or break early when y == 1) so the loop stops once p*p > y.

Suggested change
// p * p > x
if p > x / p {
// p * p > y
if p > y / p {

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments