diff options
author | mjfernez <mjfernez@gmail.com> | 2020-02-09 15:16:26 -0500 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2020-02-09 15:16:26 -0500 |
commit | 93ea7fe5957b62f18e8fbd17a21696bd7de6332d (patch) | |
tree | d90aed60d687bcf195f1150777f37cbe8a149814 /07-10001st-Prime | |
parent | 125ec5bc3d8bfc224b7d32bcfbbc37b9fb5d441f (diff) | |
download | Project_Euler_Solutions-93ea7fe5957b62f18e8fbd17a21696bd7de6332d.tar.gz |
Organized everything, update README
Diffstat (limited to '07-10001st-Prime')
-rwxr-xr-x | 07-10001st-Prime/10001prime | bin | 0 -> 16880 bytes | |||
-rw-r--r-- | 07-10001st-Prime/10001prime.c | 42 | ||||
-rw-r--r-- | 07-10001st-Prime/10001prime.py | 34 |
3 files changed, 76 insertions, 0 deletions
diff --git a/07-10001st-Prime/10001prime b/07-10001st-Prime/10001prime Binary files differnew file mode 100755 index 0000000..a9cc351 --- /dev/null +++ b/07-10001st-Prime/10001prime diff --git a/07-10001st-Prime/10001prime.c b/07-10001st-Prime/10001prime.c new file mode 100644 index 0000000..2ef3bd8 --- /dev/null +++ b/07-10001st-Prime/10001prime.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +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; +} diff --git a/07-10001st-Prime/10001prime.py b/07-10001st-Prime/10001prime.py new file mode 100644 index 0000000..bc51d49 --- /dev/null +++ b/07-10001st-Prime/10001prime.py @@ -0,0 +1,34 @@ +import PIL +import math + +# Problem 7 - 10,001st prime + +# returns a list of every prime up to a certain number + + +def listprimes(number): + primes = [] + if (number <= 1): + return [1] + a = [1] * number + a[0] = 0 + a[1] = 0 # 1 and 0 are not prime + # Sieve of eratosthenes + for i in range(2, int(math.ceil(math.sqrt(number)))): + if(a[i] == 1): + j = i**2 # cross out all the multiples of i, starting with its square + while(j < number): + a[j] = 0 + j += i + + for k in range(2, number): + # is prime + if(a[k] == 1): + primes.append(k) + return primes + + +out_list = listprimes(1000000) +# print(out_list) +# return 10,001th prime factor +print(out_list[10000]) |