Repository: TheAlgorithms/Java
Pull request #6127 (2025-01-11 18:47:10)

Added Goldbach's Conjecture algorithm.
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6127?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `0%` with `18 lines` in your changes missing coverage. Please review.
> Project coverage is 73.72%. Comparing base [(`1e6ed97`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/1e6ed97fcf96d15556466e371096b0159ffe63af?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`fb8a86c`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/fb8a86cbe11eb0f7e48d2de6e5785274992dd319?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6127?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...va/com/thealgorithms/maths/GoldbachConjecture.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6127?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fmaths%2FGoldbachConjecture.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9tYXRocy9Hb2xkYmFjaENvbmplY3R1cmUuamF2YQ==) | 0.00% | [18 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6127?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6127      +/-   ##
============================================
- Coverage     73.78%   73.72%   -0.07%     
  Complexity     5119     5119              
============================================
  Files           658      659       +1     
  Lines         17632    17650      +18     
  Branches       3391     3397       +6     
============================================
+ Hits          13010    13012       +2     
- Misses         4118     4135      +17     
+ Partials        504      503       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6127?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

BILLSARAN: > The code looks good, could you please reuse existing algorithms for primeness check and add some JUnit tests? (see https://github.com/TheAlgorithms/Java/tree/master/src/test/java/com/thealgorithms)

I think I did it, please let me know if anything else needs to be fixed

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java
new file mode 100644
index 00000000..52391bc1
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java
@@ -0,0 +1,30 @@
+package com.thealgorithms.maths;
+
+import static com.thealgorithms.maths.PrimeCheck.isPrime;
+
+/**
+ * This is a representation of the unsolved problem of Goldbach's Projection, according to which every
+ * even natural number greater than 2 can be written as the sum of 2 prime numbers
+ * More info: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture
+ * @author Vasilis Sarantidis (https://github.com/BILLSARAN)
+ */
+
+public final class GoldbachConjecture {
+    private GoldbachConjecture() {
+    }
+    public record Result(int number1, int number2) {
+    }
+
+    public static Result getPrimeSum(int number) {
+        if (number <= 2 || number % 2 != 0) {
+            throw new IllegalArgumentException("Number must be even and greater than 2.");
+        }
+
+        for (int i = 0; i <= number / 2; i++) {
+            if (isPrime(i) && isPrime(number - i)) {
+                return new Result(i, number - i);
+            }
+        }
+        throw new IllegalStateException("No valid prime sum found."); // Should not occur
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/GoldbachConjectureTest.java b/src/test/java/com/thealgorithms/maths/GoldbachConjectureTest.java
new file mode 100644
index 00000000..84c5824d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/GoldbachConjectureTest.java
@@ -0,0 +1,29 @@
+package com.thealgorithms.maths;
+
+import static com.thealgorithms.maths.GoldbachConjecture.getPrimeSum;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+public class GoldbachConjectureTest {
+    @Test
+    void testValidEvenNumbers() {
+        assertEquals(new GoldbachConjecture.Result(3, 7), getPrimeSum(10)); // 10 = 3 + 7
+        assertEquals(new GoldbachConjecture.Result(5, 7), getPrimeSum(12)); // 12 = 5 + 7
+        assertEquals(new GoldbachConjecture.Result(3, 11), getPrimeSum(14)); // 14 = 3 + 11
+        assertEquals(new GoldbachConjecture.Result(5, 13), getPrimeSum(18)); // 18 = 5 + 13
+    }
+    @Test
+    void testInvalidOddNumbers() {
+        assertThrows(IllegalArgumentException.class, () -> getPrimeSum(7));
+        assertThrows(IllegalArgumentException.class, () -> getPrimeSum(15));
+    }
+    @Test
+    void testLesserThanTwo() {
+        assertThrows(IllegalArgumentException.class, () -> getPrimeSum(1));
+        assertThrows(IllegalArgumentException.class, () -> getPrimeSum(2));
+        assertThrows(IllegalArgumentException.class, () -> getPrimeSum(-5));
+        assertThrows(IllegalArgumentException.class, () -> getPrimeSum(-26));
+    }
+}