Last active
November 4, 2020 18:04
-
-
Save thinkphp/4aacb59255c7be3e948cf167892a00eb to your computer and use it in GitHub Desktop.
Euclid's algortihm in Rust language.
This file contains 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 std::fs::File; | |
use std::io::{BufRead, BufReader, Write}; | |
fn gcd(mut x: i32, mut y: i32) -> i32 { | |
assert!(x != 0 && y != 0); | |
while y != 0 { | |
let t = x % y; | |
x = y; | |
y = t; | |
} | |
x | |
} | |
fn main() { | |
let f = File::open("euclid2.in").expect("Unable to open file"); | |
let f = BufReader::new(f); | |
let mut file_out = std::fs::File::create("euclid2.out").expect("create failed"); | |
for line in f.lines() { | |
let line = line.expect("Unable to read line"); | |
let _numbers: Vec<i32> = line | |
.split_whitespace() | |
.map(|s| s.parse().expect("parse error")) | |
.collect(); | |
if _numbers.len() == 2 { | |
let a = _numbers[0]; | |
let b = _numbers[1]; | |
let mut _c:i32 = gcd(a, b); | |
let s: String = _c.to_string() + "\n"; | |
file_out.write_all(s.as_bytes()).expect("write failed"); | |
//println!("{:?} {:?} -> {:?}", _numbers[0], _numbers[1], _c); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment