Skip to content

Commit

Permalink
Reto mouredev#21 - NÚMEROS PRIMOS GEMELOS [Media]
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgenavarroenamoradotokio authored Sep 7, 2023
1 parent 30946e9 commit 1d68e2f
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.retos.ej21;

import java.util.stream.IntStream;

public class jorgenavarroenamoradotokio {

public static void main(String[] args) {
int rangoMaximo = 14; // Cambia este valor al rango máximo deseado

System.out.println("Pares de números primos gemelos en el rango hasta " + rangoMaximo + ":");
IntStream.rangeClosed(2, rangoMaximo - 2).filter(number -> isPrime(number) && isPrime(number + 2))
.forEach(number -> System.out.println("(" + number + ", " + (number + 2) + ")"));
}

public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}

if (number <= 3) {
return true;
}

if (number % 2 == 0 || number % 3 == 0) {
return false;
}

return IntStream.rangeClosed(5, (int) Math.sqrt(number))
.allMatch(i -> number % i != 0 && number % (i + 2) != 0);
}
}

0 comments on commit 1d68e2f

Please sign in to comment.