LeetCode in Kotlin

3622. Check Divisibility by Digit Sum and Product

Easy

You are given a positive integer n. Determine whether n is divisible by the sum of the following two values:

Return true if n is divisible by this sum; otherwise, return false.

Example 1:

Input: n = 99

Output: true

Explanation:

Since 99 is divisible by the sum (9 + 9 = 18) plus product (9 * 9 = 81) of its digits (total 99), the output is true.

Example 2:

Input: n = 23

Output: false

Explanation:

Since 23 is not divisible by the sum (2 + 3 = 5) plus product (2 * 3 = 6) of its digits (total 11), the output is false.

Constraints:

Solution

class Solution {
    fun checkDivisibility(n: Int): Boolean {
        var x = n
        var sum = 0
        var mul = 1
        while (x != 0) {
            sum += x % 10
            mul *= x % 10
            x = x / 10
        }
        return n % (sum + mul) == 0
    }
}