diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-14 17:04:16 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-14 17:04:16 +0100 |
| commit | 74ac4927d7caad179c398b7bb8a54c71e783ef2e (patch) | |
| tree | 81470070f666ae7a4ddb7ece7388b206ebe1c70e /python/046-goldbachs_other_conjecture.py | |
| parent | f588a2e670f9cd92590ee535619168ca933b8098 (diff) | |
| download | project_euler-74ac4927d7caad179c398b7bb8a54c71e783ef2e.tar.gz project_euler-74ac4927d7caad179c398b7bb8a54c71e783ef2e.tar.bz2 project_euler-74ac4927d7caad179c398b7bb8a54c71e783ef2e.zip | |
problem 46 in python
Diffstat (limited to 'python/046-goldbachs_other_conjecture.py')
| -rw-r--r-- | python/046-goldbachs_other_conjecture.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/python/046-goldbachs_other_conjecture.py b/python/046-goldbachs_other_conjecture.py new file mode 100644 index 0000000..ad34df6 --- /dev/null +++ b/python/046-goldbachs_other_conjecture.py @@ -0,0 +1,54 @@ +### +# Goldbach's other conjecture +# Problem 46 +# +# 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×1^2 +# 15 = 7 + 2×2^2 +# 21 = 3 + 2×3^2 +# 25 = 7 + 2×3^2 +# 27 = 19 + 2×2^2 +# 33 = 31 + 2×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? +### + + +import itertools +import math + + +def is_prime(n): + if n == 0 or n == 1: + return False + if n == 2 or n == 3 or n == 5 or n == 7: + return True + if n % 2 == 0 or n % 3 == 0: + return False + for d in range(6, math.ceil(math.sqrt(n)) + 2, 6): + if n % (d - 1) == 0 or n % (d + 1) == 0: + return False + return True + + +for n in itertools.count(3, 2): + if is_prime(n): + continue + found = False + s = 0 + for x in itertools.count(1): + s = 2 * (x * x) + # print(n, s) + # print(s) + if s >= n: + # if not found: + # print(">>>", n) + break + if is_prime(n - s): + # print(n, math.sqrt(s / 2), n - s) + found = True + break + if not found: + print(n, math.sqrt(s / 2), n -s) + break + |
