factorial de un número

4
UNIVERSIDAD AUTÓNOMA DEL ESTADO DE MÉXICO CENTRO UNIVERSITARIO UAEM ATLACOMULCO Redes Neuronales Programa en Java Factorial Levi Guadarrama Tercero Oliver Sánchez Cruz Juan Carlos Tomas Reyes FACTORIAL Solución Para solucionar el problema del factorial, se resolverá de forma recursiva. Ejemplo: Entrada = 5 Para calcular se multiplica el numero dado por su antecesor hasta que este sea igual a cero en lo cual el factorial(1)=factorial(0)=1 5 * 4 * 3 * 2 * 1 * 0 Resultado = 120 Recibimos una variable de entrada N, luego llamamos a la subrutina Fact(X, Nivel) y luego se muestra el resultado del factorial

Upload: levy-gt

Post on 01-Jul-2015

169 views

Category:

Engineering


9 download

DESCRIPTION

Programa Factorial

TRANSCRIPT

Page 1: Factorial de un número

UNIVERSIDAD AUTÓNOMA DEL ESTADO DE MÉXICO CENTRO UNIVERSITARIO UAEM ATLACOMULCO

Redes Neuronales Programa en Java Factorial Levi Guadarrama Tercero Oliver Sánchez Cruz Juan Carlos Tomas Reyes

FACTORIAL

Solución

Para solucionar el problema del factorial, se resolverá de forma recursiva.

Ejemplo:

Entrada = 5

Para calcular se multiplica el numero dado por su antecesor hasta que este sea

igual a cero en lo cual el factorial(1)=factorial(0)=1

5 * 4 * 3 * 2 * 1 * 0

Resultado = 120

Recibimos una variable de entrada N, luego llamamos a la subrutina Fact(X, Nivel) y luego se muestra el resultado del factorial

Page 2: Factorial de un número

UNIVERSIDAD AUTÓNOMA DEL ESTADO DE MÉXICO CENTRO UNIVERSITARIO UAEM ATLACOMULCO

Redes Neuronales Programa en Java Factorial Levi Guadarrama Tercero Oliver Sánchez Cruz Juan Carlos Tomas Reyes

Dentro de la subrutina Fact(X, Nivel) se realiza pregunta

Si nivel mayor a cero Entonces Llamaremos de nuevo a Fact(Temp,Nivel-1) disminuyendo en 1 a Nivel Luego actualizamos el valor : X = Temp * Nivel

Si el nivel es menor que cero Actualizamos el valor de X = 1

Luego del cálculo retornamos el valor de x

Page 3: Factorial de un número

UNIVERSIDAD AUTÓNOMA DEL ESTADO DE MÉXICO CENTRO UNIVERSITARIO UAEM ATLACOMULCO

Redes Neuronales Programa en Java Factorial Levi Guadarrama Tercero Oliver Sánchez Cruz Juan Carlos Tomas Reyes

Código en Java

Prueba

Page 4: Factorial de un número

UNIVERSIDAD AUTÓNOMA DEL ESTADO DE MÉXICO CENTRO UNIVERSITARIO UAEM ATLACOMULCO

Redes Neuronales Programa en Java Factorial Levi Guadarrama Tercero Oliver Sánchez Cruz Juan Carlos Tomas Reyes