Created
September 2, 2012 03:11
-
-
Save brunoperezm/3594464 to your computer and use it in GitHub Desktop.
¿Cuál es el mayor de los números racionales P / Q, tales que P y Q son números primos positivos, P < Q y Q < 26662?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Problema_OMA_4 { | |
public static void main(String[] args) { | |
/*¿Cuál es el mayor de los números racionales P / Q, tales que P y Q son números primos positivos, | |
P < Q y Q < 26662?*/ | |
double mayor_P_sobre_Q = 0; | |
double current_P_Q = 0; | |
for (long q = 24000; q < 26662; q++) { | |
for(long p = 23999; p<q; p++) { | |
if (es_primo(p) && es_primo(q) ) { | |
double P = (double) p; | |
double Q = (double) q; | |
current_P_Q = P/Q ; | |
if (current_P_Q > mayor_P_sobre_Q) { | |
mayor_P_sobre_Q = current_P_Q; | |
} | |
} | |
} | |
} | |
System.out.println(mayor_P_sobre_Q); | |
} | |
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