Skip to content

Instantly share code, notes, and snippets.

@thmain
Created January 13, 2016 00:36
Show Gist options
  • Save thmain/8f93ff6f0d53e67894f2 to your computer and use it in GitHub Desktop.
Save thmain/8f93ff6f0d53e67894f2 to your computer and use it in GitHub Desktop.
public class GoldbachConjecture {
public static void Goldbach(int x) {
if (x % 2 != 0) {
System.out.println("Not Even");
return;
}
if (x <= 2) {
System.out.println("Less than 2");
return;
}
for (int i = 3; i < x / 2; i++) {
if (isPrime(i) && isPrime(x - i)) {
System.out.println("Prime Numbers are " + i + " " + (x - i));
}
}
}
public static boolean isPrime(int x) {
for (int i = 2; i < x / 2; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Goldbach(100);
}
}
@samerkanakri
Copy link

Hi
This code does not apply to 4 and 6 ,
starting the loops from 2 and ending with condition i <= x/2 will work fine 👍

123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment