Problema
n! = 1 × 2 × ... × n grows faster than any power: 25! already has 26 digits. Compute n! exactly and add up its digits. Without big integers, store the number as an array of digits and multiply it by 2, 3, ..., n in turn.
Implement factorial_digit_sum(n).
Input
n: The number whose factorial is taken.
Output
Return the sum of the decimal digits of n!. Note that 0! = 1.
Restricciones
0 ≤ n ≤ 1000
Ejemplos
Entrada:
n = 5
Salida esperada:
expected = 3
Entrada:
long long n = 5;
Salida esperada:
long long expected = 3;
Entrada:
long n = 5;
Salida esperada:
long expected = 3;
Explicación
5! = 120 and 1 + 2 + 0 = 3.
Entrada:
n = 0
Salida esperada:
expected = 1
Entrada:
long long n = 0;
Salida esperada:
long long expected = 1;
Entrada:
long n = 0;
Salida esperada:
long expected = 1;
Explicación
0! is 1 by definition, so the answer is 1.
Cómo abordarlo
Inténtalo primero por tu cuenta y luego abre las pistas una a una.
Pista 1
Start from 1 and multiply by 2, 3, ..., n.
Pista 2
Multiply each digit, add the carry, keep the last digit and carry the rest.
Pista 3
When the carry is still left over at the end, it becomes new digits.
Complejidad esperada
O(n × D) time where D is the digit count of n!, O(D) extra space
Referencias
Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.