8000 Updated prime sieve · DontFearAlgorithms/Algorithms-1@32db7a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32db7a9

Browse files
committed
Updated prime sieve
1 parent 7bab9b9 commit 32db7a9

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

com/williamfiset/algorithms/math/CompressedPrimeSieve.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
*
88
* <p>Time Complexity: ~O(nloglogn)
99
*
10-
* Compile:
11-
* javac com/williamfiset/algorithms/math/CompressedPrimeSieve.java
12-
*
13-
* Run:
14-
* java com/williamfiset/algorithms/math/CompressedPrimeSieve
10+
* <p>Compile: javac com/williamfiset/algorithms/math/CompressedPrimeSieve.java
11+
* <p>Run: java com/williamfiset/algorithms/math/CompressedPrimeSieve
1512
*
1613
* @author William Fiset, william.alexandre.fiset@gmail.com
1714
*/
@@ -29,7 +26,7 @@ private static void setBit(long[] arr, int n) {
2926

3027
// Returns true if the bit for n is off (meaning n is a prime).
3128
// Note: do use this method to access numbers outside your prime sieve range!
32-
public static boolean isNotSet(long[] arr, int n) {
29+
private static boolean isNotSet(long[] arr, int n) {
3330
if (n < 2) return false; // n is not prime
3431
if (n == 2) return true; // two is prime
3532
if ((n & 1) == 0) return false; // n is even
@@ -38,6 +35,11 @@ public static boolean isNotSet(long[] arr, int n) {
3835
return (chunk & mask) != mask;
3936
}
4037

38+
// Returns true/false depending on whether n is prime.
39+
public static boolean isPrime(long[] sieve, int n) {
40+
return isNotSet(sieve, n);
41+
}
42+
4143
// Returns an array of longs with each bit indicating whether a number
4244
// is prime or not. Use the isNotSet and setBit methods to toggle to bits for each number.
4345
public static long[] primeSieve(int limit) {
@@ -57,12 +59,14 @@ public static long[] primeSieve(int limit) {
5759
return chunks;
5860
}
5961

62+
/* Example usage. */
63+
6064
public static void main(String[] args) {
6165
final int limit = 200;
62-
long[] sieve = primeSieve(limit);
66+
long[] sieve = CompressedPrimeSieve.primeSieve(limit);
6367

64-
for (int i = 1; i <= limit; i++) {
65-
if (isNotSet(sieve, i)) {
68+
for (int i = 0; i <= limit; i++) {
69+
if (CompressedPrimeSieve.isPrime(sieve, i)) {
6670
System.out.printf("%d is prime!\n", i);
6771
}
6872
}

javatests/com/williamfiset/algorithms/datastructures/hashtable/HashTableDoubleHashingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import static org.junit.Assert.*;
44

5-
import com.williamfiset.algorithms.datastructures.hashtable.DoubleHashingTestObject; // Move to test dir?
5+
import com.williamfiset.algorithms.datastructures.hashtable.DoubleHashingTestObject; // Move to test
6+
// dir?
67
import com.williamfiset.algorithms.datastructures.hashtable.HashTableDoubleHashing;
78
import java.util.*;
89
import org.junit.*;

0 commit comments

Comments
 (0)
0