Skip to content

Instantly share code, notes, and snippets.

@eggie5
Created August 22, 2012 18:58
Show Gist options
  • Select an option

  • Save eggie5/3428377 to your computer and use it in GitHub Desktop.

Select an option

Save eggie5/3428377 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main( int argc, char *argv[])
{
int i;
int max=atoi( argv[1] );
printf("generating primes up to %d\n",max);
int nums[max];
for(i=0; i<max; i++)
{
nums[i]=i;
// printf("%d",i);
}
int p;
int j;
// O(n^2) :(
for(p=2; p<max; p++)
{
for(j=1; j*p<max; j++)
{
if(nums[(j*p)] > p)
{
nums[(j*p)]=0;
}
}
}
//print result
int k;
for(k=0;k<max;k++)
{
if(nums[k]!=0)
printf("%d\n", nums[k]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment