이경수 선생님의 수학실험실

Problem 46(Goldbach's other conjecture) 본문

Project Euler

Problem 46(Goldbach's other conjecture)

(이경수) 2019. 8. 15. 19:48

Problem 46(Goldbach's other conjecture)

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

\(9 = 7 + 2\times1^{2}\)
\(15 = 7 + 2 \times 2^{2}\)
\(21 = 3 + 2 \times 3^{2}\)
\(25 = 7 + 2 \times 3^{2}\)
\(27 = 19 + 2 \times 2^{2}\)
\(33 = 31 + 2 \times 1^{2}\)

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

 

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 issqaure(n):
    if int(math.sqrt(n)) == math.sqrt(n):
        return True

startTime = time.time()
i = 1
check = 0
primeList = [2]
while True:
    check = 0
    oddComposite = 2 * i + 1
    if isprime(oddComposite):
        primeList.append(oddComposite)
        check = 1
    else:
        for prime in primeList:
            if issqaure((oddComposite - prime) // 2):
                check = 1
                break
    if check == 0:
        print(oddComposite)
        break
    else:
        i += 1
print(time.time() - startTime, "seconds")

Run time: 0.13228797912597656 seconds

 

Solution: 5777

'Project Euler' 카테고리의 다른 글

Problem 48(Self powers)  (0) 2019.08.16
Problem 47(Distinct primes factors)  (0) 2019.08.15
Problem 45(Triangular, pentagonal, and hexagonal)  (0) 2019.08.15
Problem 44(Pentagon numbers)  (0) 2019.07.24
Problem 43(Sub-string divisibility)  (0) 2019.07.24
Comments