gcd p1
This commit is contained in:
8
gcd/Cargo.toml
Normal file
8
gcd/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "gcd"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
25
gcd/src/main.rs
Normal file
25
gcd/src/main.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
fn gcd(mut a: u64, mut b: u64) -> u64 {
|
||||
while a > 0 {
|
||||
if a < b {
|
||||
let temp = b;
|
||||
b = a;
|
||||
a = temp;
|
||||
}
|
||||
|
||||
a = a % b
|
||||
}
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gcd() {
|
||||
assert_eq!(gcd(3, 7), 1);
|
||||
assert_eq!(gcd(16, 128), 16);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("gcd(4, 6) is {}", gcd(4, 6));
|
||||
println!("gcd(7, 49) is {}", gcd(7, 49));
|
||||
println!("gcd(3, 7) is {}", gcd(3, 7));
|
||||
println!("gcd(128, 16) is {}", gcd(128, 16));
|
||||
}
|
||||
Reference in New Issue
Block a user