top of page

Algoritmos En Java Por:

Chris M. Pérez Santiago

Bubble Sort

Numeros Gemelos

Numeros Primos y Compuestos

Automata

  (Fisica)

Factorial de un numero

 

/*

 * 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 bubblesort;

 

/**

 *

 * @author chris M.

 */

import java.util.Scanner;

public class BubbleSort {

 

    /**

     * @param args the command line arguments

     */

    public static Scanner console= new Scanner(System.in);

    public static void main(String[] args) {

        

        int arreglo[]={55,774,424,2467,4131325,467575,65353,5,1,0};

        

        

        arregloOriginal(arreglo);

        System.out.println();

        bubbleSort(arreglo);

        resultado(arreglo);

        

    }

    public static int[] arregloOriginal(int arreglo[]){

        int i;

        

        for(i=0;i<arreglo.length;i++){

            System.out.print(arreglo[i]+" ");

        }

        return arreglo;

    }

    public static int[] bubbleSort(int arreglo[]){

        int i;

        int j;

        int aux;

        

        for(i=arreglo.length-1;i>0;i--){

            for(j=0;j<i;j++){

                if(arreglo[j]>arreglo[j+1]){

                    aux=arreglo[j];

                    arreglo[j]=arreglo[j+1];

                    arreglo[j+1]=aux;

                }

            }

        }

        return arreglo;

    }

    public static int[] resultado(int arreglo[]){

        int i;

        

        for(i=0;i<arreglo.length;i++){

            System.out.print(arreglo[i]+" ");

        }

        return arreglo;

    }

    

}

 

 

 

 

package numeros_gemelos;

 

/**

 *

 * @author Chris M. 

 */

import java.util.Scanner;

public class Numeros_Gemelos {

 

    public static Scanner console= new Scanner(System.in);

    public static void main(String[] args){

        

        

        int numeroInicial;

        int numeroFinal;

        int conteo=0;

        int aux=1;

        boolean esPrimo=true;

        

        

        

        System.out.print("Entre el primer valor: ");

        numeroInicial=console.nextInt();

        System.out.print("Ente el segundo valor: ");

        numeroFinal=console.nextInt();

        System.out.println();

     

        

       

       

        if(numeroInicial>numeroFinal){

            System.out.println("El primer valor no puede ser mayor del segundo valor");

            

        }else{

            

            for(int i=numeroInicial;i<=numeroFinal+1;i++){

                for(int j=1;j<=i;j++){

                    if(i%j==0){

                        conteo++;

                    }

                    if(conteo>2){

                        esPrimo=false;

                        break;

                    }

                }

                if(esPrimo){

                    if(i-aux==2){

                        System.out.println(aux+1+" - "+"("+aux+","+i+")");

                    }

                    if(verificasionPorRango(numeroInicial,numeroFinal)){

                        System.out.println("No hay números gemelos en el rango indicado");

                        break;

                        

                    }

                    aux=i;

                }

                esPrimo=true;

                conteo=0;

            }

        

        }

        

       

    }

    public static boolean verificasionPorRango(int numeroInicial,int numeroFinal){

        

        

        for(int i=numeroInicial;i<=numeroFinal;i++){

            for(int j=numeroInicial;j<=numeroFinal;j++){

                if(numeroInicial%i==0 && numeroFinal%i==0){

                    return false;

                    

                }

                if(numeroInicial%j==0 && numeroFinal%j==0){

                    return false;

                }

            }

        }

        return true;

    }

}

 

 

/*

 * 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 numerosprimos_numeroscompuestos;

 

/**

 *

 * @author Chris M.

 */

import java.util.Scanner;

public class NumerosPrimos_numerosCompuestos {

 

    /**

     * @param args the command line arguments

     */

    public static Scanner console= new Scanner(System.in);

    public static void main(String[] args) {

        

        int i;

        int j;

        

        for(i=1;i<=100;i++){

            int buscarPrimos=0;

            for(j=1;j<=i;j++){

                if(i%j==0){

                    buscarPrimos++;

                }

            }

            if(buscarPrimos==2){

                System.out.println("Primo:  \t"+i+" ");

                

            } 

            if(buscarPrimos!=2){

                System.out.println("Compuesto: \t"+i+" ");

            }

        }

    }

    

}

 

 

 

/*

 * 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 factorialdeunnumero;

 

/**

 *

 * @author Chris M.

 */

import java.util.Scanner;

public class FactorialDeUnNumero {

 

    /**

     * @param args the command line arguments

     */

    public static Scanner console= new Scanner(System.in);

    public static void main(String[] args) {

        

        int n=5;

        int factorial=1;

        int numero=n;

        

        while(n!=0){

            factorial*=n;

            n--;

        }

        System.out.println("El numero factorial de "+numero+" es "+factorial);

    }

    

}

 

 

 

 

/*

 * 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 automata;

 

/**

 *

 * @author Chris M. Perez

 */

import java.util.*;

public class Automata {

 

    int conteo;

    static char[]arreglo;

    boolean bandera;

    public static void main(String[] args) {

        

        Automata automata= new Automata();

        

        String cadena="bbb";

        arreglo=cadena.toCharArray();

        automata.inicio();

        

        

    }

    public void inicio(){

        conteo=0;

        bandera=false;

        estadoCero();

        

        

    }

    public void estadoCero(){

        System.out.println("Estado Cero");

        if(conteo<arreglo.length){

            if(arreglo[conteo]=='a'){

                conteo++;

                estadoCero();

                

            }else if(arreglo[conteo]=='b'){

                conteo++;

                estadoUno();

            }

        }

    }

    public void estadoUno(){

        System.out.println("Estado Uno");

        if(conteo<arreglo.length){

            if(arreglo[conteo]=='a'){

                conteo++;

                estadoUno();

                

            }else if(arreglo[conteo]=='b'){

                conteo++;

                estadoDos();

            }

        }

        

    }

    public void estadoDos(){

        System.out.println("Estado Dos");

        if(conteo<arreglo.length){

            if(arreglo[conteo]=='a'){

                conteo++;

                estadoUno();

                

            }else if(arreglo[conteo]=='b'){

                conteo++;

                estadoComprobasion();

            }

        }

        

    }

    public void estadoComprobasion(){

        System.out.println("Estado De Comprobasion");

        bandera=true;

        if(conteo<arreglo.length){

            if(arreglo[conteo]=='a'){

                conteo++;

                estadoComprobasion();

                

            }else if(arreglo[conteo]=='b'){

                conteo++;

                

            }

        }

    }

    public void estadoError(){

        bandera=false;

        return;

    }

    

}

 

 

 

bottom of page