A Harshad number (also called a Niven number) is a positive integer that is divisible by the sum of its own decimal digits. For example, 18 is Harshad because its digit sum is 9 and 18 is divisible by 9. Given a range, count the Harshad numbers in [L, R] inclusive.
A single line with two integers L and R.
A single integer: the number of Harshad numbers x with L <= x <= R.
Example 1
Input
1 10
Expected
10
Explanation
Every integer from 1 to 10 is divisible by its digit sum, so all 10 are Harshad numbers.
Example 2
Input
10 20
Expected
4
Explanation
In 10..20 the Harshad numbers are 10, 12, 18, and 20, giving a count of 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →