Guest User

Untitled

a guest
Dec 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1.  
  2. public class Problema_OMA_4 {
  3.  
  4. public static void main(String[] args) {
  5. /*¿Cuál es el mayor de los números racionales P / Q, tales que P y Q son números primos positivos,
  6. P < Q y Q < 26662?*/
  7. double mayor_P_sobre_Q = 0;
  8. double current_P_Q = 0;
  9. for (long q = 24000; q < 26662; q++) {
  10. for(long p = 23999; p<q; p++) {
  11. if (es_primo(p) && es_primo(q) ) {
  12. double P = (double) p;
  13. double Q = (double) q;
  14. current_P_Q = P/Q ;
  15.  
  16. if (current_P_Q > mayor_P_sobre_Q) {
  17. mayor_P_sobre_Q = current_P_Q;
  18. }
  19.  
  20. }
  21. }
  22. }
  23. System.out.println(mayor_P_sobre_Q);
  24.  
  25. }
  26. public static boolean es_primo(long x) {
  27. boolean es_primo = false;
  28. int sub_x;
  29. double raiz = Math.sqrt(x);
  30. int raiz_a = (int) raiz ;
  31. for (sub_x = 2; sub_x<raiz; sub_x++) {
  32. boolean keep_going = true;
  33. if (x%sub_x == 0) {
  34. keep_going = false; // Encotramos que no es primo
  35. }
  36. else if (sub_x == raiz_a ) { // Es el ultimo numero
  37. es_primo = true;
  38. }
  39. if (!keep_going) {
  40. break;
  41. }
  42. }
  43. return es_primo;
  44. }
  45. }
Add Comment
Please, Sign In to add comment