Skip to content

Commit 11003f7

Browse files
committed
Close....
1 parent 240c0a7 commit 11003f7

File tree

1 file changed

+70
-0
lines changed
  • Challenges_VII/Summation_Of_Four_Primes

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package Challenges_VII.Summation_Of_Four_Primes;
2+
3+
import java.util.ArrayList;
4+
import java.util.Scanner;
5+
6+
public class Main {
7+
8+
public static ArrayList<Integer> primeList;
9+
10+
public static void main(String[] args) {
11+
Scanner s = new Scanner(System.in);
12+
primeList = new ArrayList<Integer>();
13+
genPrimes();
14+
try{
15+
while (true) {
16+
String primes = "";
17+
int next = Integer.parseInt(s.nextLine().trim());
18+
if (next > 8) {
19+
if ((next - 4) % 2 == 0) {
20+
next -= 4;
21+
primes += "2 2 ";
22+
} else {
23+
next -= 5;
24+
primes += "2 3 ";
25+
}
26+
27+
for (int p : primeList) {
28+
if (primeList.contains(next - p)) {
29+
primes += p + " " + primeList.get(primeList.indexOf(next - p));
30+
break;
31+
}
32+
}
33+
34+
} else {
35+
int temp = 0;
36+
while (temp != next) {
37+
temp += 2;
38+
primes += "2 ";
39+
}
40+
}
41+
System.out.println(primes.trim());
42+
}
43+
}catch(Exception e){
44+
s.close();
45+
System.exit(0);
46+
}
47+
48+
}
49+
50+
public static void genPrimes() {
51+
primeList.add(2);
52+
for (int i = 3; i < 1000000; i++) {
53+
if (i % 2 != 0) {
54+
int rootX = (int) Math.sqrt(i);
55+
boolean divisible = false;
56+
for (int j = 2; j < rootX + 1; j++) {
57+
if (i % j == 0) {
58+
divisible = true;
59+
break;
60+
}
61+
}
62+
if (!divisible) {
63+
primeList.add(i);
64+
}
65+
66+
}
67+
}
68+
}
69+
70+
}

0 commit comments

Comments
 (0)