일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- 지오지브라
- 시뮬레이션
- 확률실험
- algeomath
- 구분구적법
- 리만합
- 삼각함수의그래프
- 몬테카를로
- 피타고라스 정리
- 재귀함수
- 오일러
- 프랙탈
- 프로젝트 오일러
- python
- counting sunday
- 수학탐구
- java
- 알지오매스
- 상합
- 이항분포
- 작도
- 하합
- project euler
- 블록코딩
- 큰수의법칙
- 제곱근의뜻
- 파이썬
- 정오각형
- 큰 수의 법칙
- Geogebra
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