Skip to content

Instantly share code, notes, and snippets.

@brunoperezm
Created September 2, 2012 04:10
Show Gist options
  • Save brunoperezm/3594775 to your computer and use it in GitHub Desktop.
Save brunoperezm/3594775 to your computer and use it in GitHub Desktop.
¿Cuántos números de nueve cifras ABCJKLRST son primos tales que ABC, JKL y RST son primos de tres cifras?
public class Problema_OMA_6 {
public static void main(String[] args) {
for (int x = 100000000 ; x<=999999999; x++) {
if (es_primo(x)) {
String X = Integer.toString(x);
String abc = X.substring(0,3);
String jkl = X.substring(3,6);
String rst = X.substring(6,9);
int ABC = Integer.parseInt(abc),
JKL = Integer.parseInt(jkl),
RST = Integer.parseInt(rst);
if(es_primo(ABC) && es_primo(JKL) && es_primo(RST)) {
System.out.println(x);
}
}
}
}
public static boolean es_primo(long x) {
boolean es_primo = false;
int sub_x;
double raiz = Math.sqrt(x);
int raiz_a = (int) raiz ;
for (sub_x = 2; sub_x<raiz; sub_x++) {
boolean keep_going = true;
if (x%sub_x == 0) {
keep_going = false; // Encotramos que no es primo
}
else if (sub_x == raiz_a ) { // Es el ultimo numero
es_primo = true;
}
if (!keep_going) {
break;
}
}
return es_primo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment