Show problems as
Classic problem #3622EasyMath

Check Divisibility by Digit Sum and Product

Real-world version: Stamped Cloakroom Ticket
Tracked in this browser only
Read Check Divisibility by Digit Sum and Product on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A cloakroom prints a positive whole number n on every ticket and stamps the lucky ones. Take the decimal digits of n as they are written, with no leading zeros. Add up those digits to get their total, multiply them together to get their product, and add the total to the product to get the ticket's key.

A ticket is stamped when n is divisible by its key. Return true if the ticket printed with n is stamped and false otherwise.

A ticket whose number contains the digit 0 has a digit product of 0, and 0 counts as divisible by any positive key, so such a ticket is stamped exactly when n is divisible by its digit total.

Examples

Example 1

Input
n = 4536
Output
true

The digits 4, 5, 3 and 6 add to 18 and multiply to 360, so the key is 378. Since 4536 is 12 times 378, the ticket is stamped.

Example 2

Input
n = 4537
Output
false

The digits 4, 5, 3 and 7 add to 19 and multiply to 420, so the key is 439, and 4537 leaves a remainder of 159 against it.

Example 3

Input
n = 1000000
Output
true

The digits add to 1 and multiply to 0, so the key is 1, and every whole number is divisible by 1.

Constraints

  • 1 <= n <= 10^6

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def is_stamped(n: int) -> bool:
Java
public boolean isStamped(int n)