Last active
January 27, 2020 12:38
-
-
Save BransonGitomeh/2841054f0767ec3dab0b338b7ada3ca1 to your computer and use it in GitHub Desktop.
GCD and LCM finding algorithim in RUST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "bran1" | |
version = "0.1.0" | |
authors = ["Your Name <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
rustc-serialize = "0.3.24" | |
serde = "1.0.104" | |
serde_json = "1.0.45" | |
primes = "0.2.4" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use primes::PrimeSet; | |
fn main() { | |
let v = vec![1, 2, 3]; | |
println!("The {:?}", calc(v)) | |
} | |
#[derive(Debug)] | |
struct Res { | |
lcm: i64, | |
gcd: i64, | |
} | |
impl PartialEq for Res { | |
fn eq(&self, other: &Self) -> bool { | |
self.lcm == other.lcm && self.gcd == other.gcd | |
} | |
} | |
fn odd(num: i64) -> bool { | |
let res = num % 2; | |
if res == 0 { | |
return true; | |
} else { | |
false | |
} | |
} | |
fn is_there_odd_number(nums: Vec<i64>) -> bool { | |
let mut odd_available = false; | |
for i in nums { | |
if odd(i) { | |
odd_available = true | |
} | |
} | |
odd_available | |
} | |
fn calc(nums: Vec<i64>) -> Res { | |
// loop through nums calculating against prime numbers | |
// checking if they are odd or even | |
if(is_there_odd_number(nums)){ | |
} | |
for i in nums { | |
println!("Testing i64 {:?} is {:?}", i, odd_or_even(i)) | |
} | |
let response = Res { lcm: 144, gcd: 6 }; | |
response | |
} | |
#[cfg(test)] | |
mod tests { | |
// Note this useful idiom: importing names from outer (for mod tests) scope. | |
use super::*; | |
#[test] | |
fn test_calc() { | |
assert_eq!(calc(vec![12, 18, 48]), Res { lcm: 144, gcd: 6 }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment