Problema
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.
Restricciones
1 ≤ n ≤ 10^18
Ejemplos
Entrada:
n = 123
Salida esperada:
expected = True
Entrada:
long long n = 123;
Salida esperada:
bool expected = true;
Entrada:
long n = 123;
Salida esperada:
bool expected = true;
Explicación
Sum 1 + 2 + 3 = 6 and product 1 × 2 × 3 = 6 are equal, so the answer is true.
Entrada:
n = 1234
Salida esperada:
expected = False
Entrada:
long long n = 1234;
Salida esperada:
bool expected = false;
Entrada:
long n = 1234;
Salida esperada:
bool expected = false;
Explicación
Sum 1 + 2 + 3 + 4 = 10 but product 1 × 2 × 3 × 4 = 24, so the answer is false.
Cómo abordarlo
Inténtalo primero por tu cuenta y luego abre las pistas una a una.
Pista 1
Walk over the digits once and update both totals.
Pista 2
Start the product at 1, not 0.
Pista 3
Any number containing a 0 digit has product 0, so it cannot be spy unless the sum is 0 too.
Complejidad esperada
O(d) time for d digits, O(1) extra space
Referencias
Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.