easy +8 pts

Harshad Number Check

Determine whether a positive integer is divisible by the sum of its digits.

A Harshad number (also called a Niven number) is an integer that is divisible by the sum of its digits. For example, 18 is a Harshad number because the sum of its digits is 1 + 8 = 9, and 18 divided by 9 equals 2 (an integer). Write a function `is_harshad(n: int) -> bool` that returns `True` if the positive integer `n` is a Harshad number, and `False` otherwise. You may assume that `n` is a positive integer (n >= 1).

Constraints

- 1 <= n <= 10^9 - The function should handle large numbers efficiently (simple digit sum and modulo is sufficient).

Example

>>> is_harshad(18)
True
>>> is_harshad(19)
False
>>> is_harshad(1)
True
>>> is_harshad(100)
True
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Compute the sum of the digits of n using a loop that repeatedly takes n % 10 and then integer-divides n by 10.
After obtaining the digit sum, check if n % digit_sum == 0.
Remember that every single-digit number is a Harshad number because its digit sum equals itself.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.