From a9404a5e85808214b7ecb35fddbc07329f0e5789 Mon Sep 17 00:00:00 2001 From: Sushrut Kane Date: Mon, 14 Jul 2025 19:19:14 +0530 Subject: [PATCH] =?UTF-8?q?Add=20goldbach=5Fconjecture.py=20implementing?= =?UTF-8?q?=20Goldbach=E2=80=99s=20Conjecture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maths/goldbach_conjecture.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 maths/goldbach_conjecture.py diff --git a/maths/goldbach_conjecture.py b/maths/goldbach_conjecture.py new file mode 100644 index 000000000000..2ffd01eff312 --- /dev/null +++ b/maths/goldbach_conjecture.py @@ -0,0 +1,23 @@ +def is_prime(n: int) -> bool: + if n < 2: + return False + for i in range(2, int(n ** 0.5) + 1): + if n % i == 0: + return False + return True + + +def goldbach(n: int) -> tuple[int, int] | None: + """ + Returns a pair of prime numbers that sum to the given even integer n, + according to Goldbach's Conjecture. + + Example: + goldbach(10) -> (3, 7) + """ + if n <= 2 or n % 2 != 0: + return None + for i in range(2, n // 2 + 1): + if is_prime(i) and is_prime(n - i): + return (i, n - i) + return None