Skip to content
Merged
Changes from 9 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
55 changes: 55 additions & 0 deletions src/main/java/com/thealgorithms/maths/GoldbachConjecture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.thealgorithms.maths;

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() {
}

/**
* Checks whether a number is prime or not
* @param n the input number
* @return true if n is prime, else return false
*/
private static boolean isPrime(int n) {
int i;
if (n <= 1 || (n % 2 == 0 && n != 2)) {
return false;
} else {
for (i = 3; i < Math.sqrt(n); i += 2) {
if (n % i == 0) {
return false;
}
}
}
return true;
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int n = scanner.nextInt();
int flag = 0;

if (n % 2 == 0 && n > 2) {
for (int i = 0; i <= n / 2 && flag == 0; i++) {
if (isPrime(i) && isPrime(n - i)) {
System.out.println(format("%d + %d = %d", i, n - i, n));
flag = 1;
}
}
} else {
System.out.println("Wrong Input");
}
}
}
Loading