You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

72 lines
978 B

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int *SieveArray;
void InitSieve(unsigned int Max)
{
unsigned int i;
for (i=0; i <= Max; i++)
{
SieveArray[i] = 1;
}
}
void Sieve(unsigned int Max)
{
unsigned int i,j;
unsigned int low_max = sqrt(Max);
for( i = 2; i <= low_max; i++)
{
for ( j = i*2; j <= Max; j += i)
{
SieveArray[j] = 0;
}
}
}
void ShowResults(unsigned int Max)
{
unsigned int i;
for (i =3; i <= Max; i++)
{
if (SieveArray[i])
{
printf("%d\n", i);
}
}
}
int main(int argc ,char **argv)
{
unsigned int Max;
if(argc < 2) {
printf("primes.sieve.c <Max>\n");
return 1;
} else {
Max = atoi(argv[1]);
}
if(Max < 1)
{
printf("Invalid Max specified\n");
return 1;
}
SieveArray = (int*) malloc(sizeof(int)*Max);
if(!SieveArray)
{
printf("Error: could not allocate SieveArray at Max:%d\n", Max);
return 1;
}
InitSieve(Max);
Sieve(Max);
ShowResults(Max);
free(SieveArray);
return 0;
}