Skip to content

Commit

Permalink
Merge pull request #3557 from nlarrea/py-branch
Browse files Browse the repository at this point in the history
Reto #21 - python
  • Loading branch information
Roswell468 authored May 24, 2023
2 parents 078ba39 + 2d60cdd commit 465f54e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/python/nlarrea.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
* 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)
"""


def get_pairs_of_primes(range_of_numbers):
pairs_of_primes = []
pair = []

for i in range(range_of_numbers + 1):
if is_prime(i):
pair.append(i)

if len(pair) == 2:
if pair[1] - pair[0] == 2:
pairs_of_primes.append(tuple(pair))

pair.pop(0)

return pairs_of_primes


def is_prime(number):
if number < 2: return False

for i in range(2, number):
if number % i == 0: return False

return True


def print_pairs(pairs):
print("\nPairs of prime numbers:")
for pair in pairs:
print(pair)


print_pairs(get_pairs_of_primes(14))

0 comments on commit 465f54e

Please sign in to comment.