Skip to content

Commit

Permalink
Merge pull request #3558 from fegorama/reto21_c
Browse files Browse the repository at this point in the history
Reto #21 - C
  • Loading branch information
Roswell468 authored May 24, 2023
2 parents 4e115f0 + 75f2d0f commit ce3b003
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/c/fegorama.c
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;
}

0 comments on commit ce3b003

Please sign in to comment.