Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weaken find_first_factor contract to any prime #325

Merged
merged 2 commits into from
Nov 19, 2024

Conversation

chiphogg
Copy link
Contributor

It turns out, none of our use cases require us to find the first
factor. All we really need is to find any prime factor. (And if the
number itself is prime, of course, then the only option will be to
return the number itself.)

Weakening this contract enables us to take advantage of faster factoring
methods that aren't guaranteed to find the smallest factor. Obviously,
we could use these faster methods to build a function that satisfies our
old contract, by repeatedly applying them to fully factor a number,
and then taking the smallest one. But this adds extra computation for
no clear benefit.

Helps #217.

It turns out, none of our use cases require us to find the _first_
factor.  All we really need is to find _any prime_ factor.  (And if the
number itself is prime, of course, then the only option will be to
return the number itself.)

Weakening this contract enables us to take advantage of faster factoring
methods that aren't guaranteed to find the _smallest_ factor.
Obviously, we could use these faster methods to build a function that
satisfies our old contract, by repeatedly applying them to _fully_
factor a number, and then taking the smallest one.  But this adds extra
computation for no clear benefit.

Helps #217.
@chiphogg chiphogg added the release notes: ♻️ lib (refactoring) Under-the-hood changes to library structure label Nov 19, 2024
@chiphogg chiphogg requested a review from geoffviola November 19, 2024 02:38
Copy link
Contributor

@geoffviola geoffviola left a comment

Choose a reason for hiding this comment

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

Relaxing the contract makes sense.

EXPECT_EQ(find_first_factor(7u * 11u * 13u), 7u);
EXPECT_EQ(find_first_factor(cube(196961u)), 196961u);
TEST(FindFactor, FindsFirstFactorWhenFirstFactorIsSmall) {
EXPECT_EQ(find_prime_factor(7u * 11u * 13u), 7u);
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: Should these tests use AnyOf as well as opposed to equaling the smallest factor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably, yeah. I was just a little bit lazy when I put the PR up initially. 🙂

@chiphogg chiphogg merged commit 3b6e979 into main Nov 19, 2024
13 checks passed
@chiphogg chiphogg deleted the chiphogg/find-prime-factor#217 branch November 19, 2024 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: ♻️ lib (refactoring) Under-the-hood changes to library structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants