#2520

Count the Digits That Divide a Number

Easy Digit extraction + modulo

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Count how many digits of num (counting repeats) divide num evenly. num contains no zero digits.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: for each digit of the number, does it divide the whole number evenly?

Why this works: walk the digits on a copy, but test divisibility on the original number.

Example: num = 1248

8: 1248 % 8 = 0 ✓4: 1248 % 4 = 0 ✓2: 1248 % 2 = 0 ✓1: 1248 % 1 = 0 ✓   → 4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Testing divisibility against the shrinking copy instead of the original number.