Smallest Divisible Digit Product I - Problem
Find the Magic Number! ๐ŸŽฏ

You are given two integers n and t. Your task is to find the smallest number greater than or equal to n such that the product of its digits is divisible by t.

For example, if n = 15 and t = 3, you need to find the smallest number โ‰ฅ 15 where multiplying all its digits gives a result divisible by 3. The number 16 has digit product 1ร—6 = 6, which is divisible by 3!

Think of it as finding the next "lucky number" where all digits multiply to create a multiple of your target value.

Input & Output

example_1.py โ€” Simple Case
$ Input: n = 15, t = 3
โ€บ Output: 16
๐Ÿ’ก Note: Starting from 15: digit product of 15 is 1ร—5=5 (not divisible by 3). Next number 16 has digit product 1ร—6=6, which is divisible by 3.
example_2.py โ€” Already Valid
$ Input: n = 23, t = 6
โ€บ Output: 23
๐Ÿ’ก Note: The number 23 itself has digit product 2ร—3=6, which is divisible by 6. So we return 23 directly.
example_3.py โ€” Zero Digit Case
$ Input: n = 101, t = 2
โ€บ Output: 111
๐Ÿ’ก Note: Number 101 has digit product 1ร—0ร—1=0, not divisible by 2. We need to find the next number without zero digits. Numbers 102-110 all contain 0. First valid is 111 with product 1ร—1ร—1=1, but 1 is not divisible by 2. Next is 112 with product 1ร—1ร—2=2, which is divisible by 2.

Visualization

Tap to expand
๐Ÿญ License Plate Factory: Finding the Magic NumberSTARTn = 15PLATE: 151 ร— 5 = 5TEST5 รท 3 = โœ—PLATE: 161 ร— 6 = 6SUCCESS!6 รท 3 = โœ“Rejection Loop: Keep trying until we find a valid plateFactory Production Steps1Start conveyor belt at number n2Calculate digit product for current plate3Check if product is divisible by lucky number t4If yes: SHIP IT! If no: try next plate
Understanding the Visualization
1
Set Production Line
Start the conveyor belt at number n, our minimum required plate number
2
Quality Check Station
For each plate, multiply all digits together to get the 'magic number'
3
Divisibility Test
Check if our magic number is divisible by the lucky number t
4
Ship or Continue
If divisible, ship the plate! Otherwise, move to the next number on the line
Key Takeaway
๐ŸŽฏ Key Insight: Like a quality control system, we systematically check each candidate until we find one that meets our mathematical requirement!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k ร— d)

In worst case similar to brute force, but often much faster in practice

n
2n
โœ“ Linear Growth
Space Complexity
O(d)

Need to store digits of the number being constructed

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 106
  • 1 โ‰ค t โ‰ค 106
  • The answer is guaranteed to exist within reasonable bounds
Asked in
Google 15 Microsoft 12 Amazon 8 Apple 6
26.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen