- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathGoldbachConjecture.java
30 lines (25 loc) · 1.01 KB
/
GoldbachConjecture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
packagecom.thealgorithms.maths;
importstaticcom.thealgorithms.maths.Prime.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)
*/
publicfinalclassGoldbachConjecture {
privateGoldbachConjecture() {
}
publicrecordResult(intnumber1, intnumber2) {
}
publicstaticResultgetPrimeSum(intnumber) {
if (number <= 2 || number % 2 != 0) {
thrownewIllegalArgumentException("Number must be even and greater than 2.");
}
for (inti = 0; i <= number / 2; i++) {
if (isPrime(i) && isPrime(number - i)) {
returnnewResult(i, number - i);
}
}
thrownewIllegalStateException("No valid prime sum found."); // Should not occur
}
}