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.
Merge pull request mouredev#3969 from cflorezp/main
Reto#21 - Java
- Loading branch information
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/java/Cflorezp.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,95 @@ | ||
package reto21PrimosGemelos; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
/* | ||
* 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 Cflorezp { | ||
|
||
public static void main(String[] args) { | ||
|
||
System.out.println("\n************************************"); | ||
System.out.println("Calculador de numeros primos Gemelos"); | ||
System.out.println("************************************\n"); | ||
System.out.println("Por favor ingrese el rango de numeros que desea: "); | ||
Scanner init = new Scanner(System.in); | ||
String value = init.nextLine(); | ||
|
||
if(validationOfNumber(value)){ | ||
int number = Integer.valueOf(value); | ||
if(number < 5){ | ||
System.out.println("El rango es muy pequeño por lo que no hay primos gemelos"); | ||
}else{ | ||
printTwins(twinsPrimes(primes(number))); | ||
} | ||
} | ||
|
||
init.close(); | ||
} | ||
|
||
public static List<Integer> primes(int number){ | ||
List<Integer> primes = new ArrayList<>(); | ||
for(int i = 3; i <= number; i++ ){ | ||
if(isPrime(i)){ | ||
primes.add(i); | ||
} | ||
} | ||
return primes; | ||
} | ||
|
||
public static boolean isPrime(int number){ | ||
int count = 0; | ||
for(int i = 2; i < number; i++){ | ||
int result = 0; | ||
result = number % i; | ||
if(result == 0){ | ||
count++; | ||
} | ||
if(count == 1){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
public static List<Integer> twinsPrimes(List<Integer> numbers){ | ||
List<Integer> twins = new ArrayList<>(); | ||
int first = 0, second = 0; | ||
for(int i = 0; i < numbers.size(); i++){ | ||
first = numbers.get(i); | ||
if((i + 1) < numbers.size()){ | ||
second = numbers.get(i + 1); | ||
if((first + 2) == second){ | ||
twins.add(first); | ||
twins.add(second); | ||
} | ||
} | ||
} | ||
return twins; | ||
} | ||
public static void printTwins(List<Integer> numbers){ | ||
for (int i = 0; i < numbers.size(); i += 2) { | ||
int element1 = numbers.get(i); | ||
int element2 = numbers.get(i + 1); | ||
System.out.print("(" + element1 + ", " + element2 + "), "); | ||
} | ||
} | ||
|
||
public static boolean validationOfNumber(String number){ | ||
if(!number.matches("[0-9]+")){ | ||
System.out.println("El valor ingresado no es valido!!"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
Retos/Reto #23 - LA BASE DE DATOS [Media]/java/Cflorezp.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,66 @@ | ||
package reto23baseDatos; | ||
|
||
import java.sql.*; | ||
|
||
/* | ||
* Realiza una conexión desde el lenguaje que hayas seleccionado a la siguiente base de datos MySQL: | ||
* - Host: mysql-5707.dinaserver.com | ||
* - Port: 3306 | ||
* - User: mouredev_read | ||
* - Password: mouredev_pass | ||
* - Database: moure_test | ||
* | ||
* Una vez realices la conexión, lanza la siguiente consulta e imprime el resultado: | ||
* - SELECT * FROM `challenges` | ||
* | ||
* Se pueden usar librerías para realizar la lógica de conexión a la base de datos. | ||
*/ | ||
public class Cflorezp { | ||
|
||
public static void main(String[] args) { | ||
String url = "jdbc:mysql://mysql-5707.dinaserver.com:3306/moure_test"; | ||
String usuario = "mouredev_read"; | ||
String contraseña = "mouredev_pass"; | ||
|
||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
|
||
Connection conexion = DriverManager.getConnection(url, usuario, contraseña); | ||
|
||
Statement statement = conexion.createStatement(); | ||
|
||
String consulta = "SELECT * FROM challenges"; | ||
ResultSet resultado = statement.executeQuery(consulta); | ||
|
||
ResultSetMetaData metaData = resultado.getMetaData(); | ||
int columnCount = metaData.getColumnCount(); | ||
|
||
System.out.println("\n-------------------------------------"); | ||
System.out.println("DATOS TABLA 'CHALLENGES'"); | ||
System.out.println("-------------------------------------"); | ||
while (resultado.next()) { | ||
for (int i = 1; i <= columnCount; i++) { | ||
String columnName = metaData.getColumnName(i); | ||
String value = resultado.getString(i); | ||
System.out.println(columnName + ": " + value); | ||
} | ||
System.out.println("-------------------------------------"); | ||
} | ||
|
||
resultado.close(); | ||
statement.close(); | ||
conexion.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|