Skip to content

3345. Smallest Divisible Digit Product I 👍

  • Time: $O(10\log n) = O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  int smallestNumber(int n, int t) {
    for (int num = n; num < n + 10; ++num)
      if (getDigitProd(num) % t == 0)
        return num;
    throw;
  }

 private:
  int getDigitProd(int num) {
    int digitProd = 1;
    while (num > 0) {
      digitProd *= num % 10;
      num /= 10;
    }
    return digitProd;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
  public int smallestNumber(int n, int t) {
    for (int num = n; num < n + 10; ++num)
      if (getDigitProd(num) % t == 0)
        return num;
    throw new IllegalArgumentException();
  }

  private int getDigitProd(int num) {
    int digitProd = 1;
    while (num > 0) {
      digitProd *= num % 10;
      num /= 10;
    }
    return digitProd;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  def smallestNumber(self, n: int, t: int) -> int:
    return next(num for num in range(n, n + 10)
                if self._getDigitProd(num) % t == 0)

  def _getDigitProd(self, num: int) -> int:
    digitProd = 1
    while num > 0:
      digitProd *= num % 10
      num //= 10
    return digitProd