Project Euler # 10 "sum of prime numbers" in Python

Problem 10 "sum of prime numbers"

The sum of prime numbers less than or equal to 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all prime numbers less than or equal to 2 million.

Python


n = 2000000

def generate_primes(n):
  primes = [2]
  for i in range(3, n+1, 2):
    for p in primes:
      if i % p == 0:
        break
    else:
      primes += [i]
    if i % (n // 100) == 1:
      print "%d / %d" % (i, n)
  return primes

primes = generate_primes(n)
result = sum(primes)

print result
print result == 142913828922
print primes[-5:]

result


142913828922
True
[1999891, 1999957, 1999969, 1999979, 1999993]

It takes too much time, so I want to make it faster.

Recommended Posts

Project Euler # 10 "sum of prime numbers" in Python
Project Euler # 13 "Sum of Large Numbers" in Python
Project Euler 10 "Sum of Prime Numbers"
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 6 "Difference in sum of squares" in Python
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 7 "1000 1st prime number" in Python
Prime numbers in Python
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Functional programming in Python Project Euler 1
[Note] Project Euler in Python (Problem 1-22)
Project Euler # 5 "Minimum Multiples" in Python
Functional programming in Python Project Euler 2
Law of large numbers in python
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
I searched for prime numbers in python
Project Euler # 14 "Longest Collatz Sequence" in Python
Discrimination of prime numbers
Find prime numbers in Python as short as possible
Prime factorization of integers entered in Python ver.1
Project Euler # 12 "High Divisibility Triangular Number" in Python
Implement sum in Python
Prime factorization ver.2 of integers input in Python
Prime number 2 in Python
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Handle prime numbers in Python / Ruby / PHP / Golang (Go)
[Python 3] Prime factorization in 14 lines
Determine prime numbers with python
Learn cumulative sum in Python
Equivalence of objects in Python
Implementation of quicksort in Python
Handle complex numbers in Python
What I learned by solving 30 questions of python Project Euler
Pixel manipulation of images in Python
Testing with random numbers in Python
Division of timedelta in Python 2.7 series
Infinite prime number generator in Python3
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
Create Python project documentation in Sphinx
Implementation of original sorting in Python
[Python] nCr mod Compute prime numbers
Project Euler 11 "Maximum product in grid"
Reversible scrambling of integers in Python
Project Euler 9 Retention of calculation results
I tried to create a list of prime numbers with python
Various ways to create an array of numbers from 1 to 10 in Python.
Conversion of string <-> date (date, datetime) in Python
Project Euler 37
Project Euler 7
Project Euler 47
Project Euler 31
Check the behavior of destructor in Python
Project Euler 4