diff options
Diffstat (limited to '10-Summation-of-Primes')
| -rwxr-xr-x | 10-Summation-of-Primes/sumofprimes | bin | 0 -> 16904 bytes | |||
| -rw-r--r-- | 10-Summation-of-Primes/sumofprimes.c | 50 | ||||
| -rw-r--r-- | 10-Summation-of-Primes/sumofprimes.py | 28 | 
3 files changed, 78 insertions, 0 deletions
| diff --git a/10-Summation-of-Primes/sumofprimes b/10-Summation-of-Primes/sumofprimesBinary files differ new file mode 100755 index 0000000..a175f6a --- /dev/null +++ b/10-Summation-of-Primes/sumofprimes diff --git a/10-Summation-of-Primes/sumofprimes.c b/10-Summation-of-Primes/sumofprimes.c new file mode 100644 index 0000000..90e0bbb --- /dev/null +++ b/10-Summation-of-Primes/sumofprimes.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +// The number to find primes up to +int n = 2000000; + +// The number of primes in the primes array +int s = 0; + +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 +	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); +    long long sum = 0; +    int len = s; +    for(int i = 0; i < s; i++){ +        sum += p[i]; +    } +	printf("%ld\n", sum); +	return 0; +} diff --git a/10-Summation-of-Primes/sumofprimes.py b/10-Summation-of-Primes/sumofprimes.py new file mode 100644 index 0000000..d0db224 --- /dev/null +++ b/10-Summation-of-Primes/sumofprimes.py @@ -0,0 +1,28 @@ +import PIL +import math + +# Problem 10 sum of primes + +def prime_factors(number): +    primes = [] +    if (number <= 1): +        return [1] +    a = [1] * number +    a[0] = 0 +    a[1] = 0  # not prime +    for i in range(2, int(math.ceil(math.sqrt(number)))): +        if(a[i] == 1): +            j = i**2  # cross out all the multiples +            while(j < number): +                a[j] = False +                j += i + +    for k in range(2, number): +        # is prime +        if(a[k] == 1): +            primes.append(k) +    return primes + + +out = prime_factors(2000000) +print(str(sum(out))) | 
