Skip to content

Commit 8f6c975

Browse files
committed
Changed output of getPrimeSum method and added exceptions, modified JUnit Tests accordingly.
1 parent 004386a commit 8f6c975

File tree

2 files changed

+26
-44
lines changed

2 files changed

+26
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.thealgorithms.maths;
22

33
import static com.thealgorithms.maths.PrimeCheck.isPrime;
4-
import static java.lang.String.format;
5-
6-
import java.util.Scanner;
74

85
/**
96
* This is a representation of the unsolved problem of Goldbach's Projection, according to which every
@@ -15,26 +12,19 @@
1512
public final class GoldbachConjecture {
1613
private GoldbachConjecture() {
1714
}
18-
19-
public static String getPrimeSum(int number) {
20-
String s1;
21-
if (number % 2 == 0 && number > 2) {
22-
for (int i = 0; i <= number / 2; i++) {
23-
if (isPrime(i) && isPrime(number - i)) {
24-
s1 = format("%d + %d = %d", i, number - i, number);
25-
return s1;
26-
}
27-
}
28-
}
29-
return "Wrong Input";
15+
public record Result(int number1, int number2) {
3016
}
3117

32-
public static void main(String[] args) {
18+
public static Result getPrimeSum(int number) {
19+
if (number <= 2 || number % 2 != 0) {
20+
throw new IllegalArgumentException("Number must be even and greater than 2.");
21+
}
3322

34-
Scanner scanner = new Scanner(System.in);
35-
System.out.println("Enter a number");
36-
int n = scanner.nextInt();
37-
String s = getPrimeSum(n);
38-
System.out.println(s);
23+
for (int i = 0; i <= number / 2; i++) {
24+
if (isPrime(i) && isPrime(number - i)) {
25+
return new Result(i, number - i);
26+
}
27+
}
28+
throw new IllegalStateException("No valid prime sum found."); // Should not occur
3929
}
4030
}
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
package com.thealgorithms.maths;
22

33
import static com.thealgorithms.maths.GoldbachConjecture.getPrimeSum;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
46

5-
import org.junit.jupiter.api.Assertions;
67
import org.junit.jupiter.api.Test;
78

89
public class GoldbachConjectureTest {
910
@Test
10-
void goldbachTestWithZero() {
11-
Assertions.assertEquals("Wrong Input", getPrimeSum(0));
11+
void testValidEvenNumbers() {
12+
assertEquals(new GoldbachConjecture.Result(3, 7), getPrimeSum(10)); // 10 = 3 + 7
13+
assertEquals(new GoldbachConjecture.Result(5, 7), getPrimeSum(12)); // 12 = 5 + 7
14+
assertEquals(new GoldbachConjecture.Result(3, 11), getPrimeSum(14)); // 14 = 3 + 11
15+
assertEquals(new GoldbachConjecture.Result(5, 13), getPrimeSum(18)); // 18 = 5 + 13
1216
}
1317
@Test
14-
void goldbachTestWithNegative() {
15-
Assertions.assertEquals("Wrong Input", getPrimeSum(-50));
18+
void testInvalidOddNumbers() {
19+
assertThrows(IllegalArgumentException.class, () -> getPrimeSum(7));
20+
assertThrows(IllegalArgumentException.class, () -> getPrimeSum(15));
1621
}
1722
@Test
18-
void goldbachTestWith2() {
19-
Assertions.assertEquals("Wrong Input", getPrimeSum(2));
20-
}
21-
@Test
22-
void goldbachTestWithOdd() {
23-
Assertions.assertEquals("Wrong Input", getPrimeSum(25));
24-
}
25-
@Test
26-
void goldbachTestEven1() {
27-
Assertions.assertEquals("3 + 5 = 8", getPrimeSum(8));
28-
}
29-
@Test
30-
void goldbachTestEven2() {
31-
Assertions.assertEquals("3 + 19 = 22", getPrimeSum(22));
32-
}
33-
@Test
34-
void goldbachTest1() {
35-
Assertions.assertEquals("3 + 7 = 10", getPrimeSum(10));
23+
void testLesserThanTwo() {
24+
assertThrows(IllegalArgumentException.class, () -> getPrimeSum(1));
25+
assertThrows(IllegalArgumentException.class, () -> getPrimeSum(2));
26+
assertThrows(IllegalArgumentException.class, () -> getPrimeSum(-5));
27+
assertThrows(IllegalArgumentException.class, () -> getPrimeSum(-26));
3628
}
3729
}

0 commit comments

Comments
 (0)