forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cesarjv
committed
Oct 22, 2023
1 parent
2813a6d
commit e05b5e9
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/java/cesarjv.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.example; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* Crea un programa que encuentre y muestre todos los pares de números primos | ||
* gemelos en un rango concreto. | ||
* El programa recibirá el rango máximo como número entero positivo. | ||
* | ||
* - Un par de números primos se considera gemelo si la diferencia entre | ||
* ellos es exactamente 2. Por ejemplo (3, 5), (11, 13) | ||
* | ||
* - Ejemplo: Rango 14 | ||
* (3, 5), (5, 7), (11, 13) | ||
*/ | ||
public class cesarjv { | ||
public static void main(String[] args) { | ||
List<Integer> primeNumbers = IntStream.range(2, 14).filter(App::isPrimeStream).boxed().toList(); | ||
Map<Integer,Integer> res=new HashMap<>(); | ||
int comparisonValue=primeNumbers.get(0); | ||
for(Integer x: primeNumbers){ | ||
if(x-comparisonValue==2){ | ||
res.put(comparisonValue,x); | ||
} | ||
comparisonValue=x; | ||
} | ||
res.forEach((k,v) -> System.out.println("("+k+", "+v+")")); | ||
} | ||
private static boolean isPrimeStream(int n) { | ||
return IntStream.range(2, n).noneMatch(i -> n % i == 0); | ||
} | ||
} |