File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
src/main/java/com/thealgorithms/maths Expand file tree Collapse file tree 1 file changed +57
-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
+ return true ;
35
+ }
36
+
37
+ public static void main (String [] args ){
38
+
39
+ Scanner scanner = new Scanner (System .in );
40
+ System .out .println ("Enter a number" );
41
+ int n = scanner .nextInt ();
42
+ int flag = 0 ;
43
+
44
+ if (n %2 == 0 && n >2 ) {
45
+ for (int i =0 ;i <=n /2 && flag ==0 ;i ++)
46
+ if (isPrime (i ))
47
+ if (isPrime (n -i ))
48
+ {
49
+ System .out .println (format ("%d+%d=%d" , i , n -i , n ));
50
+ flag =1 ;
51
+ }
52
+ }
53
+ else
54
+ System .out .println ("Wrong Input" );
55
+ }
56
+
57
+ }
You can’t perform that action at this time.
0 commit comments