Skip to content

Instantly share code, notes, and snippets.

@EightAndAHalfTails
Created November 14, 2011 17:55
Show Gist options
  • Save EightAndAHalfTails/1364601 to your computer and use it in GitHub Desktop.
Save EightAndAHalfTails/1364601 to your computer and use it in GitHub Desktop.
#include<stdio.h>
typedef enum bool {false, true} boolean ;
/* Input: an integer n > 1
*
* Let A be an array of Boolean values, indexed by integers 2 to n,
* initially all set to true.
*
* for i = 2, 3, 4, ..., while i ≤ n/2:
* if A[i] is true:
* for j = 2i, 3i, 4i, ..., while j ≤ n:
* A[j] = false
*
* Now all i such that A[i] is true are prime.
*
*/
int main(void)
{
int i , j, intmax ;
printf("Enter limit > ") ;
scanf("%d", &intmax) ;
int A[intmax] ; /*Array of integers to be Sieved*/
for( i=0 ; i<=intmax ; i++ ) /*Populate the array with consecutive integers*/
{
A[i] = true ;
}
for( i=2 ; i <= intmax ; i++)
{
if(A[i] == true)
{
printf("%d\n", i) ;
for( j=2 ; j*i<=intmax ; j++)
{
A[i*j] = false ;
}
}
}
return 0 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment