File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
src/main/java/com/thealgorithms/maths Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .thealgorithms .maths ;
2+
3+ import java .util .Scanner ;
4+
5+ import static java .lang .String .format ;
6+
7+ /**
8+ * This is a representation of the unsolved problem of Goldbach's Projection, according to which every
9+ * even natural number greater than 2 can be written as the sum of 2 prime numbers
10+ * More info: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture
11+ * @author Vasilis Sarantidis (https://github.com/BILLSARAN)
12+ */
13+
14+ public final class GoldbachConjecture {
15+ private GoldbachConjecture () {
16+ }
17+
18+ /**
19+ * Checks whether a number is prime or not
20+ * @param n the input number
21+ * @return true if n is prime, else return false
22+ */
23+ private static boolean isPrime (int n ) {
24+ int i ;
25+ if (n <= 1 || (n % 2 == 0 && n != 2 )) {
26+ return false ;
27+ }
28+ else {
29+ for (i = 3 ; i < Math .sqrt (n ); i += 2 ) {
30+ if (n % i == 0 ) {
31+ return false ;
32+ }
33+ }
34+ }
35+ return true ;
36+ }
37+
38+ public static void main (String [] args ) {
39+
40+ Scanner scanner = new Scanner (System .in );
41+ System .out .println ("Enter a number" );
42+ int n = scanner .nextInt ();
43+ int flag = 0 ;
44+
45+ if (n % 2 == 0 && n > 2 ) {
46+ for (int i = 0 ; i <= n /2 && flag == 0 ; i ++) {
47+ if (isPrime (i )) {
48+ if (isPrime (n - i )) {
49+ System .out .println (format ("%d + %d = %d" , i , n - i , n ));
50+ flag = 1 ;
51+ }
52+ }
53+ }
54+ }
55+ else {
56+ System .out .println ("Wrong Input" );
57+ }
58+ }
59+
60+ }
You can’t perform that action at this time.
0 commit comments