Skip to content

Commit d63d0b4

Browse files
committed
Changed GoldbachConjecture class and added Unit Tests for the Goldbach Conjecture algorithm
1 parent 2618a64 commit d63d0b4

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.maths;
22

3+
import static com.thealgorithms.maths.PrimeCheck.isPrime;
34
import static java.lang.String.format;
45

56
import java.util.Scanner;
@@ -15,41 +16,25 @@ public final class GoldbachConjecture {
1516
private GoldbachConjecture() {
1617
}
1718

18-
/**
19-
* Checks whether a number is prime or not
20-
* @param n the input number
21-
* @return true if n is prime, else return false
22-
*/
23-
private static boolean isPrime(int n) {
24-
int i;
25-
if (n <= 1 || (n % 2 == 0 && n != 2)) {
26-
return false;
27-
} else {
28-
for (i = 3; i < Math.sqrt(n); i += 2) {
29-
if (n % i == 0) {
30-
return false;
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;
3126
}
3227
}
3328
}
34-
return true;
29+
return "Wrong Input";
3530
}
3631

3732
public static void main(String[] args) {
3833

3934
Scanner scanner = new Scanner(System.in);
4035
System.out.println("Enter a number");
4136
int n = scanner.nextInt();
42-
int flag = 0;
43-
44-
if (n % 2 == 0 && n > 2) {
45-
for (int i = 0; i <= n / 2 && flag == 0; i++) {
46-
if (isPrime(i) && isPrime(n - i)) {
47-
System.out.println(format("%d + %d = %d", i, n - i, n));
48-
flag = 1;
49-
}
50-
}
51-
} else {
52-
System.out.println("Wrong Input");
53-
}
37+
String s = getPrimeSum(n);
38+
System.out.println(s);
5439
}
5540
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static com.thealgorithms.maths.GoldbachConjecture.getPrimeSum;
7+
8+
public class GoldbachConjectureTest {
9+
@Test
10+
void GoldbachTestWithZero() {
11+
Assertions.assertEquals("Wrong Input", getPrimeSum(0));
12+
}
13+
@Test
14+
void GoldbachTestWithNegative() {
15+
Assertions.assertEquals("Wrong Input", getPrimeSum(-50));
16+
}
17+
@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));
36+
}
37+
}

0 commit comments

Comments
 (0)