새벽에 회사에서 하던 주사위 풀이를 하려다 딴길로 빠졌습니다.
갑자기 뜬금 없는 Python 으로 간단한 문제 풀이를 했습니다. 생각보다 실행 시간이 오래 걸리네요.
그리고 문제 출처에 소수에 대한 부분이 많이 나오는거 같아 소수 판별을 함수로 분리 했습니다.
[문제 출처]
http://projecteuler.net/problem=10
[문제 내용]
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
[소스 코드]
#summationOfPrimes.py import coozplz_math import time var = 2000000 sTime = time.clock() sum = 0 for i in range(2, var-1): if(coozplz_math.isPrime(i)) : #print("PrimeNumber=", i) sum = sum + i eTime = time.clock() print("input = ", var, " sum =", sum, "time=", (eTime-sTime) * 100, "ms")
#coozplz_math.py import math def isPrime(n): result = True for i in range(2, int(math.sqrt(n)) + 1): if n%i == 0: result = False break return result
[출력 결과]
input = 20000 sum = 21171191 time= 6.096835393639568 ms
input = 200000 sum = 1709400814 time= 108.15298899502284 ms
input = 2000000 sum = 142913828922 time= 2501.15873463776 ms