Ver Mensaje Individual
  #8 (permalink)  
Antiguo 28/02/2015, 15:18
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 9 meses
Puntos: 182
Respuesta: [Ayuda]Factorial suma dígitos

Buenas,

Genial, te felicito.

Pongo ya entonces mi solución, que es un poco más compacta.

Código Java:
Ver original
  1. import java.math.BigInteger;
  2.  
  3. public class Factorial {
  4.     private static final int NUMBER = 100;
  5.  
  6.     public static void main(String[] args) {
  7.         int total = 0;
  8.         BigInteger base = new BigInteger("10");
  9.  
  10.         BigInteger factorial = BigInteger.valueOf(1);
  11.         for (int i = 1; i <= NUMBER; i++)
  12.             factorial = factorial.multiply(BigInteger.valueOf(i));
  13.  
  14.         System.out.println("Factorial: " + factorial);
  15.  
  16.         int numDigits = factorial.toString().length();
  17.  
  18.         for(int i=0; i<numDigits; i++) {
  19.             total += factorial.mod(base).intValue();
  20.             factorial = factorial.divide(base);
  21.         }
  22.         System.out.println("Total: " + total);
  23.     }
  24. }


Un saludo
__________________
If to err is human, then programmers are the most human of us