-
Notifications
You must be signed in to change notification settings - Fork 20k
Added Goldbach's Conjecture algorithm. #6127
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
Merged
+59
−0
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fb8a86c
Added Goldbach's Conjecture algorithm.
BILLSARAN d05ade3
Added Goldbach's Conjecture algorithm.
BILLSARAN 363a7b2
Added Goldbach's Conjecture algorithm.
BILLSARAN e1254ea
Added Goldbach's Conjecture algorithm.
BILLSARAN bf54316
Added Goldbach's Conjecture algorithm.
BILLSARAN 93b4e5e
Added Goldbach's Conjecture algorithm.
BILLSARAN 0ad77ae
Added Goldbach's Conjecture algorithm.
BILLSARAN 2618a64
Added Goldbach's Conjecture algorithm.
BILLSARAN ef9d447
Merge branch 'master' into GoldbachBranch1
BILLSARAN d63d0b4
Changed GoldbachConjecture class and added Unit Tests for the Goldbac…
BILLSARAN ff54b57
Merge remote-tracking branch 'origin/GoldbachBranch1' into GoldbachBr…
BILLSARAN 4caecb0
Fixed formatting issues in GoldbachConjecture and the test class
BILLSARAN 024aec2
Fixed formatting issues in GoldbachConjectureTest
BILLSARAN 004386a
Fixed formatting issues in GoldbachConjectureTest
BILLSARAN 8f6c975
Changed output of getPrimeSum method and added exceptions, modified J…
BILLSARAN ab5eb79
Merge branch 'master' into GoldbachBranch1
BILLSARAN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/com/thealgorithms/maths/GoldbachConjecture.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.