Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Last active November 4, 2020 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/4aacb59255c7be3e948cf167892a00eb to your computer and use it in GitHub Desktop.
Save thinkphp/4aacb59255c7be3e948cf167892a00eb to your computer and use it in GitHub Desktop.
Euclid's algortihm in Rust language.
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