Programming/Project Euler - python
Project Euler 10
_Sudal
2019. 2. 16. 07:02
Question:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
문제:
10 이하의 소수들의 합은 2 + 3 + 5 + 7 = 17이다.
2백만 이하의 소수들의 총합을 구하여라.
Solution:
1 2 3 4 5 6 7 8 9 10 11 | from sympy import isprime import time i = 1 start = time.time() sum = 0 while i < 2e6: if isprime(i) == True: sum += i i += 1 print(sum) print('elapsed time: {}s'.format(time.time() - start)) | cs |
sympy 의 isprime 를 이용한 풀이
3.85 초 가량 걸렸다
1 2 3 4 5 6 | from sympy import primerange import time start = time.time() print(sum(primerange(1,int(2e6+1)))) print('elapsed time: {}s'.format(time.time() - start)) | cs |
3초 걸렸다