FunctionRacerProblemsRace

Harshad Number

BeginnerMathPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Math problems