Last active
August 8, 2018 05:28
-
-
Save deyindra/d23794f436a05a4f196a to your computer and use it in GitHub Desktop.
Generate Prime table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.idey.algo.math; | |
import java.util.BitSet; | |
public class PrimeNumber { | |
private BitSet bitSet; | |
private int upperLimit; | |
public PrimeNumber(int upperLimit) { | |
this.upperLimit = upperLimit+1; | |
this.bitSet = new BitSet(this.upperLimit); | |
this.bitSet.set(0,false); | |
this.bitSet.set(1,false); | |
this.bitSet.set(2,this.upperLimit,true); | |
fillSieve(); | |
} | |
private void fillSieve(){ | |
for(int i=2;i<=upperLimit;i++){ | |
boolean isPrime = this.bitSet.get(i); | |
if(isPrime){ | |
for (int j=2;i*j<=upperLimit;j++) { | |
bitSet.set(i*j,false); | |
} | |
} | |
} | |
} | |
public boolean isPrime(int number){ | |
if(number>=upperLimit){ | |
throw new IllegalArgumentException(); | |
} | |
return this.bitSet.get(number); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment