From 8e431d287c0e89041506da4c4da57e3e3d657d72 Mon Sep 17 00:00:00 2001 From: mjfernez Date: Mon, 3 Feb 2020 23:04:10 -0500 Subject: cleanup, added c translations for some --- 10001prime.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 10001prime.c (limited to '10001prime.c') diff --git a/10001prime.c b/10001prime.c new file mode 100644 index 0000000..2ef3bd8 --- /dev/null +++ b/10001prime.c @@ -0,0 +1,42 @@ +#include +#include +#include +int n = 1000000; + +int *listPrimes(int num){ + int i; + int *primes; + int *sieve = (int *) malloc(num * sizeof(int)); + //initialize to all 1s (except 0 and 1 which are not prime) + for(i = 2; i < num; i++) + sieve[i] = 1; + for(i = 2; i < ceil(sqrt(num)); i++){ + if(sieve[i] == 1){ + int j = i * i; + while(j < num){ + sieve[j] = 0; + j += i; + } + } + } + + //now check which were prime + int s = 0; + primes = (int *) malloc(sizeof(int)); + for(i = 2; i < num; i++){ + if(sieve[i]){ + primes[s] = i; + s++; + primes = (int *) realloc (primes, (s + 1) * sizeof(int)); + } + } + + return primes; +} + +int main(){ + int *p = listPrimes(n); + int prime10001 = p[10000]; + printf("%d\n", prime10001); + return 0; +} -- cgit v1.2.3