Skip to content

Instantly share code, notes, and snippets.

@RamonLopezEscudero
Created July 6, 2016 20:47
Show Gist options
  • Save RamonLopezEscudero/c49c2976421c4a34530a75b561e45372 to your computer and use it in GitHub Desktop.
Save RamonLopezEscudero/c49c2976421c4a34530a75b561e45372 to your computer and use it in GitHub Desktop.
Búsqueda de números primos mediante el método de la Criba de Erastótenes
#include <stdio.h>
#include <stdlib.h>
// Numero tope para la búsqueda de numeros primos
#define num_data 200
// Prototipos de las funciones
void criba_erastotenes(int input_matrix[]);
void print_results(int input_matrix[]);
// Funcion principal
int main()
{
/* ---------------------------------- */
int matriz[num_data];
/* ---------------------------------- */
criba_erastotenes(matriz);
print_results(matriz);
system("pause");
return 0;
};
// Definicion de las funciones
void criba_erastotenes(int input_matrix[])
{
/* ---------------------------------- */
int i, j;
/* ---------------------------------- */
// Inicializar los dos primeros valores del arreglo en cero
input_matrix[0] = 0;
input_matrix[1] = 0;
// Llenado de la matriz con valores de 1
for(i = 2; i < num_data; i++)
{
input_matrix[i] = 1;
}
// Desarrollo de la "Criba de Erastotenes"
for(i = 2; i < num_data; i++)
{
for(j = 2; j < num_data; j++)
{
if(j != i)
{
if(j % i == 0)
{
input_matrix[j] = 0;
}
}
}
}
}
void print_results(int input_matrix[])
{
/* ---------------------------------- */
int i;
/* ---------------------------------- */
// Impresion de resultados
for(i = 0; i < num_data; i++)
{
if(input_matrix[i] != 0)
{
printf("El numero %i es un numero primo \n", i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment