Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the Goldbach's Conjecture algorithm #6126

Closed
wants to merge 3 commits into from
Closed
Changes from all 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
57 changes: 57 additions & 0 deletions src/main/java/com/thealgorithms/maths/GoldbachConjecture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.thealgorithms.maths;

import java.util.Scanner;

import static java.lang.String.format;

/**
* 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))
if(isPrime(n - i))
{
System.out.println(format("%d + %d = %d", i, n - i, n));
flag = 1;
}
}
else
System.out.println("Wrong Input");
}

}
Loading