Skip to content

Instantly share code, notes, and snippets.

@dplord
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dplord/927ecee7b070bf82b2f5 to your computer and use it in GitHub Desktop.
Save dplord/927ecee7b070bf82b2f5 to your computer and use it in GitHub Desktop.
oneMillionPrime.c
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#define MAX 1000000000
int main()
{
bool *state = (bool *)malloc(sizeof(bool) * (MAX+1));
int64_t sq = sqrt(MAX);
for(int64_t i = 2; i<= sq; ++ i)
{
if(!state[i])
{
for(int64_t j = i*i; j <= MAX; j += i) {
state[j] = true;
}
}
}
int64_t num = 0, sum = 0;
for(int64_t i = 2; i <= MAX; ++ i)
{
if(!state[i]) {
num ++;
sum += i;
}
}
free(state);
printf("num = %lld, sum = %lld\n ", num, sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment