Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto#21 - Java #3969

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Retos/Reto #23 - LA BASE DE DATOS [Media]/java/Cflorezp.java
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();
}
}
}