Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.trapito.tutorialTrapito;
  7.  
  8. import java.io.BufferedWriter;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.util.Arrays;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16.  *
  17.  * @author Mrivem
  18.  */
  19. public class NewClass {
  20.     public static void main(String[] args) throws IOException {
  21.        
  22.         // Creo variables
  23.         long[] maximos = new long[]{0,0,0}; // array que contendrá los 3 valores maximos
  24.         long maximo = 0; // valor maximo actual
  25.         int indice = 0; // indice que se movera por el array
  26.        
  27.        
  28.         // array de aqui a la quebrá de ají
  29.         for(long i = 1; i <= Long.MAX_VALUE; i++){
  30.             // determino el factorial
  31.             long factorial = factorial(i);
  32.             System.out.format("%s! es %s\n", i, factorial);
  33.            
  34.             // si el valor se va a underflow, salir del loop
  35.             if(factorial <= 0){
  36.                 break;
  37.             }
  38.             // si no, continuar
  39.             else {
  40.                 // Si el valor actual es mayor que el maximo
  41.                 if(factorial > maximo){
  42.                     // re asigno el maximo
  43.                     maximo = factorial;
  44.                     // guardar en el array
  45.                     maximos[indice] = factorial;
  46.                     // mover el indice y retroceder a 0 si me paso
  47.                     indice++;
  48.                     if(indice == maximos.length) indice = 0;
  49.                 }
  50.             }
  51.         }
  52.         // Ordeno el array y lo imprimo
  53.         Arrays.sort(maximos);
  54.         System.out.println(Arrays.toString(maximos));
  55.        
  56.         // Creo un archivo de texto
  57.         BufferedWriter outputWriter = new BufferedWriter(new FileWriter("maximos.txt"));
  58.         // para cada item del array
  59.         for (int i = 0; i < maximos.length; i++) {
  60.             try {
  61.                 // lo escribo en el archivo
  62.                 outputWriter.write(Long.toString(maximos[i]));
  63.                 outputWriter.newLine();
  64.             } catch (IOException ex) {
  65.                 Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
  66.             }
  67.            
  68.         }
  69.         // Guardo cambios y cierro el archivo
  70.         outputWriter.flush();  
  71.         outputWriter.close();
  72.     }
  73.    
  74.     public static long factorial(long num){
  75.         // 1! = 1, caso de salida de la funcion recursiva
  76.         if(num == 1){
  77.             return 1;
  78.         }
  79.         // An! = An * An-1!
  80.         return num * factorial(num - 1);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement