-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeGames.java
More file actions
67 lines (61 loc) · 1.27 KB
/
PrimeGames.java
File metadata and controls
67 lines (61 loc) · 1.27 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
//cbasurto: hw 2 problem 3 prime games
public class PrimeGames {
public static void main(String[] args) {
listTwinPrimes(15);
System.out.println();
listGoldbach(12);
}
public static boolean isPrime(int x){
boolean isPrime=true;
int divisor=2;
if (x<2)
{isPrime=false;}
else if(x>2)
while(divisor <= Math.sqrt(x) && isPrime){
if(x%divisor==0)
{isPrime=false;}
divisor++;
}
return isPrime;
}
//twinPrime static method
public static boolean twinPrime(int x) {
if(isPrime(x) && isPrime(x+2)) {
return true;
}
else {
return false;
}
}
//listTwinPrimes static method
public static void listTwinPrimes(int x) {
int i;
for(i=3;i<=x-2;i++) {
if(twinPrime(i)) {
System.out.println(i+", "+(i+2));
}
}
}
//golbbach static method
public static String goldbach(int e) {
if(e%2 != 0) {
return "";
}
int i;
for(i=2;i<=e/2;i++) {
if(isPrime(i)&&isPrime(e-i)) {
return (e+"="+i+"+"+(e-i));
}
}
return "";
}
//listing the Goldbach
public static void listGoldbach(int x) {
int i;
for(i=4;i<=x;i++) {
String gret = goldbach(i);
if(!gret.isEmpty())
System.out.println(gret);
}
}
}