Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/main/java/com/thealgorithms/maths/GoldbachConjecture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.thealgorithms.maths;

import static com.thealgorithms.maths.PrimeCheck.isPrime;
import static java.lang.String.format;

import java.util.Scanner;

/**
* 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 static String getPrimeSum(int number) {
String s1;
if (number % 2 == 0 && number > 2) {
for (int i = 0; i <= number / 2; i++) {
if (isPrime(i) && isPrime(number - i)) {
s1 = format("%d + %d = %d", i, number - i, number);
return s1;
}
}
}
return "Wrong Input";
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int n = scanner.nextInt();
String s = getPrimeSum(n);
System.out.println(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thealgorithms.maths;

import static com.thealgorithms.maths.GoldbachConjecture.getPrimeSum;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class GoldbachConjectureTest {
@Test
void goldbachTestWithZero() {
Assertions.assertEquals("Wrong Input", getPrimeSum(0));
}
@Test
void goldbachTestWithNegative() {
Assertions.assertEquals("Wrong Input", getPrimeSum(-50));
}
@Test
void goldbachTestWith2() {
Assertions.assertEquals("Wrong Input", getPrimeSum(2));
}
@Test
void goldbachTestWithOdd() {
Assertions.assertEquals("Wrong Input", getPrimeSum(25));
}
@Test
void goldbachTestEven1() {
Assertions.assertEquals("3 + 5 = 8", getPrimeSum(8));
}
@Test
void goldbachTestEven2() {
Assertions.assertEquals("3 + 19 = 22", getPrimeSum(22));
}
@Test
void goldbachTest1() {
Assertions.assertEquals("3 + 7 = 10", getPrimeSum(10));
}
}
Loading