Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.trapito.tutorialTrapito;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Mrivem
- */
- public class NewClass {
- public static void main(String[] args) throws IOException {
- // Creo variables
- long[] maximos = new long[]{0,0,0}; // array que contendrá los 3 valores maximos
- long maximo = 0; // valor maximo actual
- int indice = 0; // indice que se movera por el array
- // array de aqui a la quebrá de ají
- for(long i = 1; i <= Long.MAX_VALUE; i++){
- // determino el factorial
- long factorial = factorial(i);
- System.out.format("%s! es %s\n", i, factorial);
- // si el valor se va a underflow, salir del loop
- if(factorial <= 0){
- break;
- }
- // si no, continuar
- else {
- // Si el valor actual es mayor que el maximo
- if(factorial > maximo){
- // re asigno el maximo
- maximo = factorial;
- // guardar en el array
- maximos[indice] = factorial;
- // mover el indice y retroceder a 0 si me paso
- indice++;
- if(indice == maximos.length) indice = 0;
- }
- }
- }
- // Ordeno el array y lo imprimo
- Arrays.sort(maximos);
- System.out.println(Arrays.toString(maximos));
- // Creo un archivo de texto
- BufferedWriter outputWriter = new BufferedWriter(new FileWriter("maximos.txt"));
- // para cada item del array
- for (int i = 0; i < maximos.length; i++) {
- try {
- // lo escribo en el archivo
- outputWriter.write(Long.toString(maximos[i]));
- outputWriter.newLine();
- } catch (IOException ex) {
- Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // Guardo cambios y cierro el archivo
- outputWriter.flush();
- outputWriter.close();
- }
- public static long factorial(long num){
- // 1! = 1, caso de salida de la funcion recursiva
- if(num == 1){
- return 1;
- }
- // An! = An * An-1!
- return num * factorial(num - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement