-
Notifications
You must be signed in to change notification settings - Fork 3k
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 #3557 from nlarrea/py-branch
Reto #21 - python
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/python/nlarrea.py
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,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)) |