Advertisement
javipinero

Emirps Java Tuenti Contest

Jun 21st, 2011
1,526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package emirps;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.LinkedList;
  7.  
  8. public class Emirps {
  9.  
  10.     public LinkedList<Integer> listEmirps = new LinkedList<Integer>();
  11.  
  12.     public Emirps(int valor) {
  13.         obtenerEmirps(valor);
  14.     }
  15.  
  16.     public Boolean[] obtenerPrimos(int valor) {
  17.  
  18.         Boolean arrayNum[] = new Boolean[valor + 1];
  19.  
  20.         arrayNum[0] = false;
  21.         arrayNum[1] = true;
  22.         if (valor < 2) {
  23.             return arrayNum;
  24.         }
  25.         arrayNum[2] = true;
  26.  
  27.         boolean siguienteValor = true;
  28.  
  29.         for (int i = 3; i <= valor; i++) {
  30.             arrayNum[i] = siguienteValor;
  31.             siguienteValor = !siguienteValor;
  32.         }
  33.  
  34.         for (int i = 3; i <= Math.sqrt(valor); i = i + 2) {
  35.             if (arrayNum[i] == true) {
  36.                 for (int j = i * 2; j <= valor; j = j + i) {
  37.                     arrayNum[j] = false;
  38.                 }
  39.             }
  40.         }
  41.         return arrayNum;
  42.     }
  43.  
  44.     public static boolean esPrimo(int numero) {
  45.  
  46.         if (numero == 1 || numero == 2) {
  47.             return true;
  48.         }
  49.  
  50.         if (numero == 0 || numero % 2 == 0) {
  51.             return false;
  52.         }
  53.  
  54.         for (int i = 3; i <= Math.sqrt(numero); i = i + 2) {
  55.             if (numero % i == 0) {
  56.                 return false;
  57.             }
  58.         }
  59.         return true;
  60.     }
  61.  
  62.     public void obtenerEmirps(int valor) {
  63.  
  64.         Boolean numPrimos[] = obtenerPrimos(valor);
  65.         for (int i = 10; i <= valor; i++) {
  66.             if (numPrimos[i]) {
  67.                 int numReves = darVuelta(i);
  68.                 if (numReves != i) {
  69.                     if (numReves <= valor) {
  70.                         if (numPrimos[numReves]) {
  71.                             listEmirps.add(i);
  72.                         }
  73.                     } else if (esPrimo(numReves)) {
  74.                         listEmirps.add(i);
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81.     public static int darVuelta(int numero) {
  82.         int aux = 0;
  83.  
  84.         while (numero > 0) {
  85.             aux = aux * 10 + numero % 10;
  86.             numero /= 10;
  87.         }
  88.  
  89.         return aux;
  90.     }
  91.  
  92.     public static void main(String[] args) {
  93.  
  94.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  95.  
  96.         try {
  97.             while (br.ready()) {
  98.                 int valor = 0;
  99.                 String line = br.readLine();
  100.                 line = line.trim();
  101.                 Emirps emirps = new Emirps(Integer.parseInt(line));
  102.                 while (!emirps.listEmirps.isEmpty()) {
  103.                     valor += emirps.listEmirps.getFirst();
  104.                     emirps.listEmirps.remove();
  105.                 }
  106.                 System.out.println(valor);
  107.             }
  108.         } catch (IOException e) {
  109.             e.printStackTrace();
  110.         }
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement