From 60d89e8681b0f5e81705dd0f0a748461dc82d3e5 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sun, 2 Oct 2022 18:37:34 +0200 Subject: [PATCH] Adopted new convention from template --- src/Python/Problems001-050/Problem046.py | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Python/Problems001-050/Problem046.py b/src/Python/Problems001-050/Problem046.py index 2b2d479..5a912d0 100644 --- a/src/Python/Problems001-050/Problem046.py +++ b/src/Python/Problems001-050/Problem046.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python """ Created on 12 Sep 2021 @@ -9,40 +9,42 @@ Solution for problem 46 of Project Euler https://projecteuler.net/problem=46 """ -from utils import timeit, is_prime +from utils import is_prime, timeit + def is_goldbach(number): for i in range(number - 1, 1, -1): - if is_prime(i) and ((number - i) / 2)**0.5 % 1 == 0: + 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 + 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 + 9 = 7 + 2x1^2 + 15 = 7 + 2x2^2 + 21 = 3 + 2x3^2 + 25 = 7 + 2x3^2 + 27 = 19 + 2x2^2 + 33 = 31 + 2x1^2 It turns out that the conjecture was false. - What is the smallest odd composite that cannot be written as the sum + 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 + print(f"Result for Problem 46 is {compute()}")