Skip to content

Commit d225b36

Browse files
committed
Add problem 3345: Smallest Divisible Digit Product I
1 parent 922beac commit d225b36

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,7 @@ pub mod problem_3334_find_the_maximum_factor_score_of_array;
21982198
pub mod problem_3335_total_characters_in_string_after_transformations_i;
21992199
pub mod problem_3337_total_characters_in_string_after_transformations_ii;
22002200
pub mod problem_3340_check_balanced_string;
2201+
pub mod problem_3345_smallest_divisible_digit_product_i;
22012202
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
22022203

22032204
#[cfg(test)]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::num::NonZero;
6+
7+
impl Solution {
8+
pub fn smallest_number(n: i32, t: i32) -> i32 {
9+
let mut n = n.cast_unsigned();
10+
let t = NonZero::new(t.cast_unsigned()).unwrap();
11+
12+
while {
13+
let mut product = 1;
14+
let mut x = n;
15+
16+
while x != 0 {
17+
product *= x % 10;
18+
x /= 10;
19+
}
20+
21+
product % t != 0
22+
} {
23+
n += 1;
24+
}
25+
26+
n.cast_signed()
27+
}
28+
}
29+
30+
// ------------------------------------------------------ snip ------------------------------------------------------ //
31+
32+
impl super::Solution for Solution {
33+
fn smallest_number(n: i32, t: i32) -> i32 {
34+
Self::smallest_number(n, t)
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
#[test]
41+
fn test_solution() {
42+
super::super::tests::run::<super::Solution>();
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod brute_force;
2+
3+
pub trait Solution {
4+
fn smallest_number(n: i32, t: i32) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [((10, 2), 10), ((15, 3), 16)];
13+
14+
for ((n, t), expected) in test_cases {
15+
assert_eq!(S::smallest_number(n, t), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)