-
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 #3558 from fegorama/reto21_c
Reto #21 - C
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/c/fegorama.c
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,52 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int isPrime(int n) | ||
{ | ||
if (n < 3 || n == 4) | ||
{ | ||
return 0; | ||
} | ||
|
||
int max = n / 2; | ||
|
||
for (int i=2; i < max; i++) | ||
{ | ||
if (n % i == 0) | ||
return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int main(int args, char** argv) | ||
{ | ||
int rg, diff, ant = 0; | ||
|
||
if (args != 2) | ||
{ | ||
printf("Debe introducir el rango máximo."); | ||
return 1; | ||
} | ||
|
||
rg = atoi(argv[1]); | ||
|
||
if (rg > 4) { | ||
printf("(3, 5)"); | ||
} else | ||
return 0; | ||
|
||
for (int i=4; i <= rg; i++) { | ||
if (isPrime(i)) { | ||
diff = i - ant; | ||
|
||
if (diff == 2) { | ||
printf(", (%d, %d)", ant, i); | ||
} | ||
|
||
ant = i; | ||
} | ||
} | ||
|
||
return 0; | ||
} |