Intuition¶
The check is a direct restatement of the definition: compute the digit sum $$S$$ and
the digit product $$P$$ of n, then test whether n is divisible by $$P + S$$. Both
quantities are folds over the same sequence of decimal digits, so a single scan that
updates two accumulators is enough — there is no need to store the digits.
Approach: Digit Extraction¶
- Initialize
product = 1andsum = 0, and copyninto a working variabletemp. - While
temp > 0, takedigit = temp % 10, multiply it intoproduct, add it tosum, then drop it withtemp /= 10. - Return whether
n % (product + sum) == 0.
The peel relies on two standard identities: temp % 10 is the last digit of temp,
and integer division temp /= 10 removes it. The loop ends exactly when every digit
has been consumed. The seed values are the respective identities — product = 1
(seeding it with 0 would zero out every result) and sum = 0. Digits are visited
right-to-left, which is harmless because both addition and multiplication are
commutative.
Worked example: n = 99 → true¶
| iter | digit | sum | product | temp after |
|---|---|---|---|---|
| 1 | 9 | 9 | 9 | 9 |
| 2 | 9 | 18 | 81 | 0 |
Divisor = 81 + 18 = 99, and 99 % 99 == 0, so the answer is true.
Worked example: n = 23 → false¶
| iter | digit | sum | product | temp after |
|---|---|---|---|---|
| 1 | 3 | 3 | 3 | 2 |
| 2 | 2 | 5 | 6 | 0 |
Divisor = 6 + 5 = 11, and 23 % 11 == 1, so the answer is false.
Worked example: n = 105 → false¶
| iter | digit | sum | product | temp after |
|---|---|---|---|---|
| 1 | 5 | 5 | 5 | 10 |
| 2 | 0 | 5 | 0 | 1 |
| 3 | 1 | 6 | 0 | 0 |
The zero digit collapses product to 0 permanently. Divisor = 0 + 6 = 6, and
105 % 6 == 3, so the answer is false.
Consequences worth noting¶
- A zero digit removes the product entirely. For any
ncontaining a0, the test degenerates to "isndivisible by its digit sum?". - Digit sum
1always returnstrue. For10,100, ...,1000000the divisor is1, and every integer is divisible by1. - No single-digit
never returnstrue. A lone digitdgives divisor $$d + d = 2d$$, and $$d \bmod 2d = d \ne 0$$ for every $$d \ge 1$$. So1,5, and9all returnfalse. - The divisor is never zero. Since $$n \ge 1$$ the leading digit is at least
1, hence $$S \ge 1$$; with $$P \ge 0$$ this gives $$P + S \ge 1$$ and the modulo is always well defined. - No 32-bit overflow. Under $$n \le 10^6$$ the digit product peaks at
$$9^6 = 531441$$ (at
999999), far below thei32limit, so Rust'si32arithmetic is safe without widening.
Complexity¶
- Time complexity: $$O(\log_{10} n)$$ — one iteration per decimal digit, at most 7 iterations for $$n \le 10^6$$.
- Space complexity: $$O(1)$$ — three scalar accumulators, no digit buffer.
Converting n to a string and folding over its characters is equally correct but
allocates; the arithmetic peel avoids that allocation entirely.
Code¶
Go¶
func checkDivisibility(n int) bool {
temp := n
product, sum := 1, 0
for temp > 0 {
digit := temp % 10
product *= digit
sum += digit
temp /= 10
}
return n % (product + sum) == 0
}
Rust¶
impl Solution {
pub fn check_divisibility(n: i32) -> bool {
let (mut product, mut sum, mut temp) = (1, 0, n);
while temp > 0 {
let digit = temp % 10;
product *= digit;
sum += digit;
temp /= 10;
}
n % (product + sum) == 0
}
}
Test cases¶
n |
product | sum | divisor | n % divisor |
result |
|---|---|---|---|---|---|
99 |
81 | 18 | 99 | 0 | true |
23 |
6 | 5 | 11 | 1 | false |
1 |
1 | 1 | 2 | 1 | false |
9 |
9 | 9 | 18 | 9 | false |
10 |
0 | 1 | 1 | 0 | true |
20 |
0 | 2 | 2 | 0 | true |
12 |
2 | 3 | 5 | 2 | false |
36 |
18 | 9 | 27 | 9 | false |
105 |
0 | 6 | 6 | 3 | false |
1000000 |
0 | 1 | 1 | 0 | true |
Sweeping the full constraint range, 54669 of the 1000000 values in
$$[1, 10^6]$$ return true.