This commit is contained in:
2022-08-16 01:03:04 +05:30
commit 1f75203146
3 changed files with 127 additions and 0 deletions

8
gcd/Cargo.toml Normal file
View 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
View 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));
}