- 問題
Problem 10:Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.Find the sum of all the primes below two million.
- 解答例
def sieveEratosthenes(num): isPrime = [True] * (num + 1) isPrime[0] = False isPrime[1] = False primes = [] for i in range(2, (num + 1)): if isPrime[i]: primes.append(i) for j in range(2 * i, (num + 1), i): isPrime[j] = False return primes primeArray = sieveEratosthenes(2000000) sum = 0 for i in primeArray: sum += i print(sum)