-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathegyptian-multiplication.rs
More file actions
47 lines (40 loc) · 1.14 KB
/
egyptian-multiplication.rs
File metadata and controls
47 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::io;
macro_rules! parse_input {
($t:ident) => {{
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
input_line.trim_matches('\n').parse::<$t>().unwrap()
}}
}
fn main() {
let inputs = parse_input!(String)
.split(' ')
.map(|x| x.parse::<i32>().unwrap_or(0))
.collect::<Vec<_>>();
let mut b = core::cmp::min(inputs[0], inputs[1]);
let mut a = core::cmp::max(inputs[0], inputs[1]);
let mut decomp = Vec::new();
println!("{} * {}", a, b);
while 0 != b {
match b % 2 {
0 => {
a = a * 2;
b = (b + 1)/2;
},
_ => {
decomp.push(a);
b = b - 1;
},
}
let decomp_str = if decomp.is_empty() {
String::new()
} else {
format!(" + {}", decomp.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(" + "))
};
println!("= {} * {}{}", a, b, decomp_str);
}
println!("= {}", decomp.iter().sum::<i32>());
}