From 75f2d0fc9ad0f4313507d6c9288f767d81bf90cd Mon Sep 17 00:00:00 2001 From: Fegor Date: Tue, 23 May 2023 18:39:11 +0200 Subject: [PATCH] Reto #21 - C --- .../c/fegorama.c" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/c/fegorama.c" diff --git "a/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/c/fegorama.c" "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/c/fegorama.c" new file mode 100644 index 0000000000..a6be7868d9 --- /dev/null +++ "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/c/fegorama.c" @@ -0,0 +1,52 @@ +#include +#include + +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; +}