Last active
August 29, 2015 14:19
-
-
Save dplord/927ecee7b070bf82b2f5 to your computer and use it in GitHub Desktop.
oneMillionPrime.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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