Problem
A spy number is a positive integer whose digit sum equals its digit product. For example, 1124 has sum 1 + 1 + 2 + 4 = 8 and product 1 × 1 × 2 × 4 = 8, so it is a spy number.
Implement spy_number(n).
Input
n: The number to test.
Output
Return true if the sum of the decimal digits of n equals their product, otherwise false. Every single-digit number is a spy number.
Constraints
1 ≤ n ≤ 10^18
Examples
Input:
n = 123
Expected output:
expected = True
Input:
long long n = 123;
Expected output:
bool expected = true;
Input:
long n = 123;
Expected output:
bool expected = true;
Explanation
Sum 1 + 2 + 3 = 6 and product 1 × 2 × 3 = 6 are equal, so the answer is true.
Input:
n = 1234
Expected output:
expected = False
Input:
long long n = 1234;
Expected output:
bool expected = false;
Input:
long n = 1234;
Expected output:
bool expected = false;
Explanation
Sum 1 + 2 + 3 + 4 = 10 but product 1 × 2 × 3 × 4 = 24, so the answer is false.
How to approach it
Try on your own first, then open one hint at a time.
Hint 1
Walk over the digits once and update both totals.
Hint 2
Start the product at 1, not 0.
Hint 3
Any number containing a 0 digit has product 0, so it cannot be spy unless the sum is 0 too.
Expected complexity
O(d) time for d digits, O(1) extra space
References
This challenge is adapted for CodeUtility. Follow the statement and constraints above.