Skip to content

Define extended GCD, combined GCD+LCM - #19

Merged
bors[bot] merged 9 commits into
rust-num:masterfrom
strake:gcd
Apr 25, 2019
Merged

Define extended GCD, combined GCD+LCM#19
bors[bot] merged 9 commits into
rust-num:masterfrom
strake:gcd

Conversation

@strake

@strakestrake commented Dec 18, 2018

Copy link
Copy Markdown
Contributor

No description provided.

@strake
strakeforce-pushed the gcd branch 5 times, most recently from 6accdbc to f6f06f8CompareDecember 19, 2018 05:43
@strake

Copy link
Copy Markdown
ContributorAuthor

Rebased, PTAL

@cuvipercuviper left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please cargo fmt this, and if it skips the macro impls, please format them manually.

cc #10 and #12 that overlap this PR.

Comment threadsrc/lib.rs Outdated
#[cfg_attr(array_clone, derive(Clone))]
pub struct ExtendedGcd<A> {
pub gcd: A,
pub coeffs: [A; 2],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's use separate x and y, so we can document it something like this:

Bézout's identity — Let a and b be integers with greatest common divisor d. Then, there exist integers x and y such that ax + by = d.

Then we also don't have to worry about array_clone.

Comment threadsrc/lib.rs Outdated
let q = r.1.clone() / r.0.clone();
let f = |r: &mut (Self, Self)| {
mem::swap(&mut r.0, &mut r.1);
r.0 -= q.clone() * r.1.clone();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, it's a little annoying to have SubAssign in the API just for this, and AFAICT it's only needed because you're working through &mut. If the closure used values instead, or if you just flattened these three calls, then you could move and then reassign r.0 just fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we should require Signed though -- I believe it's useless for unsigned integers, since one of the coefficients must be negative, except the degenerate case with zeros.

@strake

Copy link
Copy Markdown
ContributorAuthor

@cuviper All done, PTAL

@cuviper

Copy link
Copy Markdown
Member

Thanks!

bors r+

borsBot added a commit that referenced this pull request Apr 25, 2019
19: Define extended GCD, combined GCD+LCM r=cuviper a=strake
Co-authored-by: M Farkas-Dyck <strake888@gmail.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors

borsBot commented Apr 25, 2019

Copy link
Copy Markdown
Contributor

Build failed

@cuviper

Copy link
Copy Markdown
Member

Sorry, that was my fault.

bors r+

borsBot added a commit that referenced this pull request Apr 25, 2019
19: Define extended GCD, combined GCD+LCM r=cuviper a=strake
Co-authored-by: M Farkas-Dyck <strake888@gmail.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors

borsBot commented Apr 25, 2019

Copy link
Copy Markdown
Contributor

Build succeeded

Sign up for freeto 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.

3 participants

@strake@cuviper@error256