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 - Python #5854

Merged
merged 1 commit into from
Nov 25, 2023
Merged
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
42 changes: 42 additions & 0 deletions Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/python/sublian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Reto #21: Números primos gemelos
#### Dificultad: Media | Publicación: 22/05/23 | Corrección: 29/05/23 | Mi solución: 24/11/2023

## Enunciado
# 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)

#creacion de funcion que revisa si es primo o no
def is_prime(number: int)->bool:
#definamos caso base
if number < 2:
return False

#revisamos si es un numero primo
for index in range(2, number):
if number%index ==0:
return False
return True

#mostramos los primos gemelos
def find_prime_twins(range_number: int):
if range_number >2:
print(f"Valores gemelos primos para <{range_number}>")
for index in range(2, range_number):
#revisa si el valor no excede el rango y valida si cada elemento es primo
if index +2 <=range_number and is_prime(index) and is_prime(index+2):
print(f"({index}, {index+2})")
else:
print(f"No hay numeros primos gemelos para <{range_number}>")

if __name__ == "__main__":

find_prime_twins(42)
find_prime_twins(2)
find_prime_twins(-100)
find_prime_twins(int(input("Ingresa un valor para evaluar la existencia de # primos gemelos: ")))