-
Notifications
You must be signed in to change notification settings - Fork 0
/
p046.py
49 lines (40 loc) · 941 Bytes
/
p046.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 2 12:10:13 2020
@author: zhixia liu
"""
"""
Project Euler 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×12
15 = 7 + 2×22
21 = 3 + 2×32
25 = 7 + 2×32
27 = 19 + 2×22
33 = 31 + 2×12
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?
"""
#%% naive
from helper import prime_list
from math import sqrt
import sys
prime = prime_list()
def isdoublesquare(n):
t = sqrt(n/2)
return t.is_integer()
n = 7
while True:
n += 2
if prime.isPrime(n):
continue
prime.generateToN(n)
for p in prime:
if p>n:
print(n)
sys.exit(0)
if isdoublesquare(n-p):
break
else:
print(n)
break