I have solved the question 10 regarding sum of all primes under 2 million, however my code takes over a few minutes to calculate the result.
I was just wondering if there is any way to optimise it to make it run faster ?
- The code takes an upper limit
- Generates an array
- Iterates through it and removes multiples of a number, replacing it with 0
- Takes that filtered array and loops through the next non zero number
- Increases this number till it is sqrt of the limit.
- Prints out what is left.
import numpy as np
def sievePrime(n):
array = np.arange(2, n)
tempSieve = [2]
for value in range(2, int(np.floor(np.sqrt(n)))):
if tempSieve[value - 2] != value:
continue
else:
for x in range(len(array)):
if array[x] % value == 0 and array[x] != value:
array[x] = 0
tempSieve = array
return sum(array)
print(sievePrime(2000000))
Thank you for your time.
source https://stackoverflow.com/questions/74603473/a-faster-solution-for-project-euler-question-10
Comments
Post a Comment