-
Notifications
You must be signed in to change notification settings - Fork 38
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
Add lcm and gcd functions #15
Comments
An implementation: extern crate ramp;
use std::cmp::Ordering;
use ramp::Int;
#[derive(Debug,Copy,Clone,PartialEq,Eq)]
struct GcdResult<T> {
/// Greatest common divisor.
gcd: T,
/// Coefficients such that: gcd(a, b) = c1*a + c2*b
c1: T, c2: T,
}
/// Calculate greatest common divisor and the corresponding coefficients.
fn extended_gcd(a: Int, b: Int) -> GcdResult<Int> {
// Euclid's extended algorithm
let (mut s, mut old_s) = (Int::zero(), Int::one());
let (mut t, mut old_t) = (Int::one(), Int::zero());
let (mut r, mut old_r) = (b, a);
while r != 0 {
let quotient = &old_r / &r;
let mut tmp;
tmp = r.clone(); r = &old_r - "ient * r; old_r = tmp;
tmp = s.clone(); s = &old_s - "ient * s; old_s = tmp;
tmp = t.clone(); t = &old_t - "ient * t; old_t = tmp;
}
let _quotients = (t, s); // == (a, b) / gcd
GcdResult { gcd: old_r, c1: old_s, c2: old_t }
}
fn main() {
println!("{:?}", extended_gcd(Int::from(6), Int::from(3)));
} From this it is possible to calculate a few other number theoretical functions. Where should this functionality go? (By the way, I think everything should be destructive, adding a |
@vks I'd really like to see this implemented in the low-level section to avoid as much copying. Probably in a new file Since GCD is integral to implementing rationals, I want to make sure it's reasonably efficient in it's first implementation. Doesn't have to be super-tuned like GMP's right away, but just implementing it in the unsafe portion of the library should be enough. As for what the higher-level API should be, I think that a static method on |
Euclid's extended algorithm is not a good fit for multiple-precision integers, see http://cacr.uwaterloo.ca/hac/about/chap14.pdf Section 14.4 for more details. I think it would be a good idea to implement one or more of the algorithms, which are described in this section. |
I have implemented the binary gcd and the extended binary gcd algorithms, as described in the "Handbook of applied cryptography". Both run two times faster as Euclid's extended algorithm. |
I have seen, that other libraries (like GMP) are using different algorithms for the various input sizes. See also: |
I don't know yet when I'll find the time to implement the low-level version. (I've had a quick look at low-level ramp, and frankly it made the task at hand seem a bit daunting.) |
The only thing missing to close this issue is a safe interface to Edit: Nevermind, those are implemented. It would be nice to have extended Euclid algorithm though, to get the coefficients. |
I like the idea of a low-level extended gcd function. I may try to implement it in the future. |
No description provided.
The text was updated successfully, but these errors were encountered: