-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoldbachConjecture.java
55 lines (46 loc) · 1.14 KB
/
GoldbachConjecture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main;
import java.util.ArrayList;
/**
*
* @author Ivan Petrov
*
*/
public class GoldbachConjecture {
private static ArrayList<Integer> generatePrimeNumbers(int number) {
ArrayList<Integer> primeNumebers = new ArrayList<Integer>();
for (int i = 2; i < number / 2 + 1; i++) {
if (isPrime(i)) {
primeNumebers.add(i);
}
}
return primeNumebers;
}
private static boolean isPrime(int number){
if (number <= 1) {
return false;
} else if (number == 2) {
return true;
} else if (number % 2 == 0) {
return false;
}
for(int i = 3; i < Math.sqrt(number * 1.0); i+=2){
if(number % i == 0){
return false;
}
}
return true;
}
public static void goldbach(int number) {
ArrayList<Integer> primeNumbers = generatePrimeNumbers(number);
int prime = 0;
for (int i = 0; i < primeNumbers.size(); i++) {
prime = number - primeNumbers.get(i);
if (isPrime(prime)) {
System.out.printf("(%d, %d)",primeNumbers.get(i), prime);
}
}
}
public static void main(String[] args) {
goldbach(100);
}
}