diff options
Diffstat (limited to 'sumofprimes.py')
-rw-r--r-- | sumofprimes.py | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/sumofprimes.py b/sumofprimes.py index 3799e94..e85e394 100644 --- a/sumofprimes.py +++ b/sumofprimes.py @@ -1,8 +1,9 @@ -import PIL, math +import PIL +import math -#Problem 10 sum of primes +# Problem 10 sum of primes ###INEFFICIENT### -#def isPrime(number): +# def isPrime(number): # if (number <=1): # return False # for i in range(2,number): @@ -10,35 +11,35 @@ import PIL, math # return False # return True -#def prime_factors(number): - #primes = [] - #for i in range(number, 2, -1): - #if(number%i==0): - #if (isPrime(i)): - #return i - #return primes - +# def prime_factors(number): +#primes = [] +# for i in range(number, 2, -1): +# if(number%i==0): +# if (isPrime(i)): +# return i +# return 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 + 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))) |