#3622
Easy Algorithms Check divisibility by digit sum and product
Math
69.7% acceptance
Feb 25, 2026
51
0
You are given a positive integer n. Determine whether n is divisible by the sum of the following two values:
The digit sum of n (the sum of its digits).
The digit product of n (the product of its digits).
Return true if n is divisible by this sum; otherwise, return false.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn check_divisibility(n: i32) -> bool {
let mut s = 0i64;
let mut p = 1i64;
let mut tmp = n;
while tmp > 0 {
let d = (tmp % 10) as i64;
s += d;
p *= d;
tmp /= 10;
}
let denom = s + p;
denom != 0 && n as i64 % denom == 0
}
}