Published: October 6, 2023

Project Euler

Problem 10 - Summation of Primes

Problem

This problem comes from Project Euler 10

Problem

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all primes below two million.

Solution

We can once again use the Sieve of Eratosthenes as seen in Problem 3. Since the sieve returns a list (array), we can just sum the returned list.

Code

# Project Euler: Problem 10
# Summation of primes

from PEF import soe

primes = soe(2000000)
print(sum(primes))
# PEF: Project Euler Functions

import math

def soe(n):
    '''Sieve of Eratosethenes. Finds prime numbers'''

    primes = []
    number_list = [True]*(n+1)

    for i in range(2, n+1):
        if number_list[i]:
            primes.append(i)

            # Eliminate multiples. They aren't prime
            for multiple in range(i**2, n+1, i):
                number_list[multiple] = False
    
    return primes