Prime

質數:恰好有兩個因數的整數,一個是1,另一個則是它自己,比如整數3和5就是質數。質數的基本算法有素性測試、埃氏篩法和整數分解。

質數測試

如果dn的因數,則易知 n = d \cdot \frac{n}{d}, 因此 n/d也是n的因數,且這兩個因數中的較小者 \min(d, n/d) <= \sqrt{n}. 因此我們只需要對前 \sqrt{n} 個數進行處理。

埃氏篩法 Sieve of Eratosthenes

質數測試針對的是單個整數,如果需要枚舉整數n以內的質數就需要埃氏篩法了。核心思想是枚舉從小到大的質數並將質數的整數倍依次從原整數數組中刪除,餘下的即爲全部質數。

區間篩法

求區間[a, b)內有多少質數?

埃氏篩法得到的是[1, n)內的質數,求區間質數時不太容易直接求解,我們採取以退爲進的思路先用埃氏篩法求得[1, b)內的質數,然後截取爲[a, b)即可。

Implementation

Java

  1. import java.util.*;
  2. public class Prime {
  3. // test if n is prime
  4. public static boolean isPrime(int n) {
  5. for (int i = 2; i * i <= n; i++) {
  6. if (n % i == 0) return false;
  7. }
  8. return n != 1; // 1 is not prime
  9. }
  10. // enumerate all the divisor for n
  11. public static List<Integer> getDivisor(int n) {
  12. List<Integer> result = new ArrayList<Integer>();
  13. for (int i = 1; i * i <= n; i++) {
  14. if (n % i == 0) {
  15. result.add(i);
  16. // i * i <= n ==> i <= n / i
  17. if (i != n / i) result.add(n / i);
  18. }
  19. }
  20. Collections.sort(result);
  21. return result;
  22. }
  23. // 12 = 2 * 2 * 3, the number of prime factor, small to big
  24. public static Map<Integer, Integer> getPrimeFactor(int n) {
  25. Map<Integer, Integer> result = new HashMap<Integer, Integer>();
  26. for (int i = 2; i * i <= n; i++) {
  27. // if i is a factor of n, repeatedly divide it out
  28. while (n % i == 0) {
  29. if (result.containsKey(i)) {
  30. result.put(i, result.get(i) + 1);
  31. } else {
  32. result.put(i, 1);
  33. }
  34. n = n / i;
  35. }
  36. }
  37. // if n is not 1 at last
  38. if (n != 1) result.put(n, 1);
  39. return result;
  40. }
  41. // sieve all the prime factor less equal than n
  42. public static List<Integer> sieve(int n) {
  43. List<Integer> prime = new ArrayList<Integer>();
  44. // flag if i is prime
  45. boolean[] isPrime = new boolean[n + 1];
  46. Arrays.fill(isPrime, true);
  47. isPrime[0] = false;
  48. isPrime[1] = false;
  49. for (int i = 2; i <= n; i++) {
  50. if (isPrime[i]) {
  51. prime.add(i);
  52. for (int j = 2 * i; j <= n; j += i) {
  53. isPrime[j] = false;
  54. }
  55. }
  56. }
  57. return prime;
  58. }
  59. // sieve between [a, b)
  60. public static List<Integer> sieveSegment(int a, int b) {
  61. List<Integer> prime = new ArrayList<Integer>();
  62. boolean[] isPrime = new boolean[b];
  63. Arrays.fill(isPrime, true);
  64. isPrime[0] = false;
  65. isPrime[1] = false;
  66. for (int i = 2; i < b; i++) {
  67. if (isPrime(i)) {
  68. for (int j = 2 * i; j < b; j += i) isPrime[j] = false;
  69. if (i >= a) prime.add(i);
  70. }
  71. }
  72. return prime;
  73. }
  74. public static void main(String[] args) {
  75. if (args.length == 1) {
  76. int n = Integer.parseInt(args[0]);
  77. if (isPrime(n)) {
  78. System.out.println("Integer " + n + " is prime.");
  79. } else {
  80. System.out.println("Integer " + n + " is not prime.");
  81. }
  82. System.out.println();
  83. List<Integer> divisor = getDivisor(n);
  84. System.out.print("Divisor of integer " + n + ":");
  85. for (int d : divisor) System.out.print(" " + d);
  86. System.out.println();
  87. System.out.println();
  88. Map<Integer, Integer> primeFactor = getPrimeFactor(n);
  89. System.out.println("Prime factor of integer " + n + ":");
  90. for (Map.Entry<Integer, Integer> entry : primeFactor.entrySet()) {
  91. System.out.println("prime: " + entry.getKey() + ", times: " + entry.getValue());
  92. }
  93. System.out.print("Sieve prime of integer " + n + ":");
  94. List<Integer> sievePrime = sieve(n);
  95. for (int i : sievePrime) System.out.print(" " + i);
  96. System.out.println();
  97. } else if (args.length == 2) {
  98. int a = Integer.parseInt(args[0]);
  99. int b = Integer.parseInt(args[1]);
  100. List<Integer> primeSegment = sieveSegment(a, b);
  101. System.out.println("Prime of integer " + a + " to " + b + ":");
  102. for (int i : primeSegment) System.out.print(" " + i);
  103. System.out.println();
  104. }
  105. }
  106. }