2
1
3
4
5

2
2
2 3
2 3 5

#### 解題概念

• 把要拿來測試的除數分為單數和偶數兩個部份的話，偶數只需拿「2」來測試即可，因為能整除以「4」、「6」、「8」等等偶數的數必定也能整除以「2」。
• 最大的除數只需取到被除數開根號的無條件捨棄值即可，因為如果有大於或等於這個值的數可以整除被除數，必定又有小於或等於這個值的數可以整除被除數。

#### 參考答案

```import java.io.BufferedReader;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import static java.lang.System.in;

class Prime {

private static final int MAX_PRIME_TABLE = 10000;
private static final TreeSet<Integer> primeTable = new TreeSet<>();

static {
for (int i = 1; i <= MAX_PRIME_TABLE; i += 6) {
final int i6 = i * 6;
final int a = i6 - 1;
final int b = i6 + 1;
if (isPrime(a)) {
}
if (isPrime(b)) {
}
}
}

private static boolean isPrime(final int n) {
if (n % 2 == 0) {
return false;
}
final int sqrtN = (int) Math.sqrt(n);
for (int i = 3; i <= sqrtN; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}

public void checkPrime(final int... n) {
final StringBuilder sb = new StringBuilder();
final int length = n.length;
for (int i = 0; i < length; ++i) {
final int number = n[i];
boolean isPrime = false;
if (number > MAX_PRIME_TABLE) {
final int m = number % 6;
isPrime = (m == 1 || m == 5) && isPrime(number);
} else {
isPrime = primeTable.contains(number);
}

if (isPrime) {
sb.append(number).append(' ');
}
}
System.out.println(sb.toString().trim());
}
}

public class Solution {

public static void main(final String[] args) throws Exception {
try {
final Prime ob = new Prime();
ob.checkPrime(n1);
ob.checkPrime(n1, n2);
ob.checkPrime(n1, n2, n3);
ob.checkPrime(n1, n2, n3, n4, n5);
final Method[] methods = Prime.class.getDeclaredMethods();
final Set<String> set = new HashSet<>();
for (int i = 0; i < methods.length; i++) {
if (set.contains(methods[i].getName())) {