University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sieve.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * I use 0 to represent prime and 1 to represent not prime,
  3. * because a byte[] is initialized to all 0.
  4. *
  5. * Stores prime/not prime in all 8 bits in each byte.
  6. */
  7. import java.util.ArrayList;
  8. class Sieve {
  9. public byte[] arr;
  10. public int maxNum;
  11. public boolean smallPrimesGenerated = false;
  12. private int sqrtNum;
  13. private int numPrimes = 0;
  14. private int[] primes = null;
  15. Sieve(int maxNum, byte[] arr) {
  16. this.maxNum = maxNum;
  17. this.sqrtNum = (int)Math.ceil(Math.sqrt(maxNum));
  18. this.arr = arr;
  19. }
  20. Sieve(int maxNum) {
  21. this(maxNum, new byte[(maxNum / 16) + 1]);
  22. }
  23. public int[] getPrimes() {
  24. if (primes == null) {
  25. primes = new int[(maxNum / ((int)Math.log(maxNum) - 1)) + 1];
  26. int i = 0;
  27. primes[i++] = 2;
  28. for (int p = 3; p < maxNum; p += 2) {
  29. if (isPrime(p))
  30. primes[i++] = p;
  31. }
  32. }
  33. return primes;
  34. }
  35. // Find all prime numbers.
  36. public void generatePrimes(int step, int offset) {
  37. if (!smallPrimesGenerated)
  38. generateSmallPrimes();
  39. for (int i = 2 + offset; i <= sqrtNum; i += (step * 2)) {
  40. if (isPrime(i)) {
  41. setNotPrimes(i, maxNum);
  42. }
  43. }
  44. }
  45. public void generatePrimes() {
  46. generatePrimes(1, 1);
  47. }
  48. // Set all multiples of i as not primes
  49. private void setBigPrimes(int i) {
  50. int j = 1;
  51. }
  52. // Find the first sqrt(n) prime numbers, and set them
  53. // as not prime.
  54. public void generateSmallPrimes() {
  55. smallPrimesGenerated = true;
  56. for (int i = 3; i <= sqrtNum; i += 2) {
  57. if (isPrime(i))
  58. setNotPrimes(i, sqrtNum);
  59. }
  60. }
  61. // Check whether a number is prime (aka if its bit is 0)
  62. public boolean isPrime(int i) {
  63. if (i == 2)
  64. return true;
  65. if ((i & 1) == 0)
  66. return false;
  67. byte b = arr[byteIndex(i)];
  68. return (b & (1 << bitIndex(i))) == 0;
  69. }
  70. // Set all multiples of i under sqrt(n) as not primes
  71. private void setNotPrimes(int min, int max) {
  72. int j = min;
  73. min *= 2;
  74. int mul = 3;
  75. while (min <= max) {
  76. setNotPrime(min);
  77. min = j * mul;
  78. mul += 2;
  79. }
  80. }
  81. // Get the byte a number's bit is in
  82. private int byteIndex(int i) {
  83. return i >> 4;
  84. }
  85. // Get what bit in the byte a number is in
  86. private int bitIndex(int i) {
  87. return (i % 16) >> 1;
  88. }
  89. // Set a number to be not prime (aka set its bit to 1)
  90. private void setNotPrime(int i) {
  91. // No need to store even numbers
  92. if (i % 2 == 0)
  93. return;
  94. int bi = byteIndex(i);
  95. arr[bi] = (byte)(arr[bi] | (1 << bitIndex(i)));
  96. }
  97. }