/
iterative.rs
41 lines (32 loc) · 919 Bytes
/
iterative.rs
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
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl Solution {
pub fn subtract_product_and_sum(n: i32) -> i32 {
let mut n = n as u32;
let mut product = 1;
let mut sum = 0;
loop {
let digit = (n % 10) as i32;
n /= 10;
product *= digit;
sum += digit;
if n == 0 {
break;
}
}
product - sum
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn subtract_product_and_sum(n: i32) -> i32 {
Self::subtract_product_and_sum(n)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}