Harshad Number
A Harshad number is divisible by the sum of its own digits.
If x is a Harshad number, return the sum of its digits. Otherwise return -1.
Example 1
x 18
returns 9
Digit sum 9 divides 18, so return 9.
Example 2
x 23
returns -1
Digit sum 5 does not divide 23.
Constraints
1 <= x <= 10**5
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Math problems
- Hex String to DecimalBeginner
- Highest Power of TwoBeginner
- Is PrimeBeginner
- Lattice Points in a CircleBeginner
- Leap YearBeginner
- Matches in a TournamentBeginner
- Number of Common FactorsBeginner
- Power of FourBeginner