일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Geogebra
- 오일러
- 확률실험
- 몬테카를로
- 큰 수의 법칙
- 하합
- 이항분포
- 지오지브라
- 수학탐구
- 상합
- 제곱근의뜻
- java
- 프랙탈
- project euler
- 재귀함수
- 시뮬레이션
- 파이썬
- 피타고라스 정리
- python
- 리만합
- 블록코딩
- 큰수의법칙
- 구분구적법
- 프로젝트 오일러
- 알지오매스
- 정오각형
- algeomath
- 삼각함수의그래프
- 작도
- counting sunday
Archives
- Today
- Total
이경수 선생님의 수학실험실
Problem 50(Consecutive prime sum) 본문
Problem 50(Consecutive prime sum)
The prime 41, can be written as the sum of six consecutive primes:
\(41 = 2 + 3 + 5 + 7 + 11 + 13\)
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
In Python:
import time
import math
def isprime(n):
if n == 0 or n == 1:
return False
else:
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
def seive(n):
primeList = []
pcodeList = [1 for i in range(0, n)]
pcodeList[0] = 0
for i in range(2, n + 1):
if pcodeList[i - 1] == 1:
primeList.append(i)
for j in range(2, (n // i) + 1):
pcodeList[i * j - 1] = 0
return primeList
startTime = time.time()
primeList = seive(10 ** 6)
total = 0
count = 0
resultList = []
countList = []
for i in range(0, len(primeList) - 1):
count = 0
if i == 0:
total = 0
j = i
while total < 10 ** 6 and j < len(primeList) - 1:
total += (primeList[j] + primeList[j + 1])
count += 1
j += 2
if isprime(total) and total < 10 ** 6:
resultList.append(total)
countList.append(count)
else:
total = primeList[i]
j = i + 1
while total < 10 ** 6 and j < len(primeList) - 1:
total += (primeList[j] + primeList[j + 1])
count += 1
j += 2
if isprime(total) and total < 10 ** 6:
resultList.append(total)
countList.append(count)
print(resultList[countList.index(max(countList))], max(countList))
print(time.time() - startTime, "seconds")
Run time: 6.135763883590698 seconds
In Python:
import time
import math
def isprime(n):
if n == 0 or n == 1:
return False
else:
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
def seive():
n = 10 ** 6
i = 2
total = 0
primeList = []
pcodeList = [1 for i in range(0, n)]
pcodeList[0] = 0
while total < 10 ** 6:
if pcodeList[i - 1] == 1:
primeList.append(i)
total += i
for j in range(2, (n // i) + 1):
pcodeList[i * j - 1] = 0
i += 1
return primeList[0: i - 1]
startTime = time.time()
primeList = seive()
total = 0
maxcount = 0
result = 0
for i in range(len(primeList) - 1):
total = primeList[i]
j = i + 1
while total < 10 ** 6 and j < len(primeList) - 1:
total += primeList[j]
if isprime(total) and total < 10 ** 6:
if j - i + 1 > maxcount:
maxcount = j - i + 1
result = total
j += 1
print(result, maxcount)
print(time.time() - startTime, "seconds")
Run time: 1.1931350231170654 seconds
Solution: 997651
'Project Euler' 카테고리의 다른 글
Problem 51(Prime digit replacements) (0) | 2019.08.18 |
---|---|
Problem 49(Prime permutations) (0) | 2019.08.16 |
Problem 48(Self powers) (0) | 2019.08.16 |
Problem 47(Distinct primes factors) (0) | 2019.08.15 |
Problem 46(Goldbach's other conjecture) (0) | 2019.08.15 |
Comments