From c034f9743152bf076c343f18474ae7ea7b429eb6 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Thu, 16 Sep 2021 19:04:29 +0200 Subject: [PATCH] Solution to problem 46 --- src/Python/Problem046.py | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/Python/Problem046.py diff --git a/src/Python/Problem046.py b/src/Python/Problem046.py new file mode 100644 index 0000000..2b2d479 --- /dev/null +++ b/src/Python/Problem046.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +""" +Created on 12 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for problem 46 of Project Euler +https://projecteuler.net/problem=46 +""" + +from utils import timeit, is_prime + +def is_goldbach(number): + for i in range(number - 1, 1, -1): + if is_prime(i) and ((number - i) / 2)**0.5 % 1 == 0: + return True + return False + +@timeit("Problem 46") +def compute(): + """ + 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? + """ + ans = 9 + while True: + ans += 2 + if not is_prime(ans) and not is_goldbach(ans): + return ans + + + +if __name__ == "__main__": + + print(f"Result for Problem 46: {compute()}") \ No newline at end of file