public class PrimeSieve extends Object
This class uses an implementation of the Sieve of Eratosthenes to get all the prime numbers less or equal than a given number.
This implementation handles numbers up to 3*237-577
i.e. (Integer.MAX_VALUE - 2) * Long.SIZE * 3 -1
= 412316859839.
This class keeps the bit-array used to compute the Sieve of Eratosthenes so, if the requested prime number is very big, this class needs and keeps allocated a lot of memory. To tell if the required N is prime it needs up to 2*N/3 bits (therefore up to N/12 bytes) of memory. For example, to tell if 1000000181 is prime (by the way, it is), you need about 80MB of memory.
The limit of 3*237-577 is based on the fact that the JVM does
not allow to allocate an array with a size greater than Integer.MAX_VALUE - 2,
but the platform-specific limits can be more restrictive, for example
the VM may not have enough memory.
If the required N is too big, you may run into a OutOfMemoryError.
For example finding all primes less than 3*237-576, besides being
relatively time consuming, it requires 16Gb of memory.
You can use the method getMemoryConsumptionInBytes(long) to know
the memory occupation given the upper limit and you can use
the method getGreatestComputableValue(long)
to know the greatest value that can be computed using the given amount of memory.
| Modifier and Type | Field and Description |
|---|---|
static long |
MAX_VALUE
The greatest value theoretically handled by this class is 3*237-577
i.e.
|
static int |
MIN_MEMORY_CONSUMPTION_IN_BYTES
The minimum amount of memory allocated by this object when new created.
|
| Modifier | Constructor and Description |
|---|---|
protected |
PrimeSieve(long maxValue)
Constructor with parameters.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object other) |
static PrimeSieve |
get()
Returns a new
PrimeSieve with the default limits:
Greatest computable value: 412316859839
Memory occupation upper bound: 16GB
|
long |
getCurrentMemoryConsumptionInBytes()
Returns the amount of memory, in bytes, currently used by this object.
|
static long |
getGreatestComputableValue(long memoryLimitInBytes)
Returns the the greatest value that can be computed
with the given amount of memory expressed in bytes.
|
long |
getGreatestComputedValue()
Returns the greatest value computed so far.
|
long |
getGreatestPrimeLessEqual(long value)
Returns the greatest prime number less or equal than the provided value.
|
long |
getMaxValue()
Returns the greatest value computable by this instance.
|
static long |
getMemoryConsumptionInBytes(long upperLimit)
Returns the maximum amount of memory (in bytes) needed
to compute all primes less or equal than the given upper limit.
|
long |
getSmallestPrimeGreaterEqual(long value)
Returns the smallest prime number greater or equal than the provided value.
|
int |
hashCode() |
boolean |
isPrime(long value)
Tells if the given value is a prime number.
|
String |
toString() |
static PrimeSieve |
withMemoryLimit(long memoryLimitInBytes)
Returns a new
PrimeSieve that will use up to the
given amount of memory. |
static PrimeSieve |
withUpperLimit(long upperLimit)
Returns a new
PrimeSieve that will compute prime
numbers up to the given value. |
public static final long MAX_VALUE
public static final int MIN_MEMORY_CONSUMPTION_IN_BYTES
protected PrimeSieve(long maxValue)
maxValue - the greatest computable value.public static PrimeSieve get()
PrimeSieve with the default limits:
41231685983916GBPrimeSievepublic static PrimeSieve withUpperLimit(long upperLimit)
PrimeSieve that will compute prime
numbers up to the given value.
The provided value cannot be greater than 3*237-577, otherwise an exception will be thrown.
If the method isPrime(long) is invoked with a
value greater than this limit an exception will be thrown.
upperLimit - the greatest computable value.PrimeSieveRequirementFailure - if the upper limit is greater than 3*237-577public static PrimeSieve withMemoryLimit(long memoryLimitInBytes)
PrimeSieve that will use up to the
given amount of memory.
The minimum amount of memory used by an instance of this class
is MIN_MEMORY_CONSUMPTION_IN_BYTES. So, if the given
memory limit is less than MIN_MEMORY_CONSUMPTION_IN_BYTES,
it will be ignored.
If the method isPrime(long) is invoked with a
value that requires, for being computed, more memory than the
given limit, an exception will be thrown.
memoryLimitInBytes - amount of memory in bytes that
this instance is allowed to use.PrimeSievepublic static long getMemoryConsumptionInBytes(long upperLimit)
In any case the given value must not be greater than 3*237-577.
upperLimit - the upper limit of the values to compute.RequirementFailure - if the upper limit is greater than 3*237-577public static long getGreatestComputableValue(long memoryLimitInBytes)
memoryLimitInBytes - number of bytes of available memory.RequirementFailure - if the amount of available memory is not positive.public boolean isPrime(long value)
The given value must belong to the interval [0,getMaxValue()].
The computation for value N takes up to N/12 bytes of memory.
If the prime values in the interval [0,value] have been computed
this method takes O(1), otherwise it takes O(N)
value - the value to check.true if it is a prime number, false otherwise.OutOfMemoryError - if the required memory exceeds the available one.RequirementFailure - if the value is greater than getMaxValue().public long getSmallestPrimeGreaterEqual(long value)
If there are no prime numbers grater or equal than the given threshold
within the internal bounds the value -1 will be returned.
If the prime values in the interval [0,value] have been computed
this method takes O(1), otherwise it takes O(N)
value - the threshold value.-1 otherwise.OutOfMemoryError - if the required memory exceeds the available one.RequirementFailure - if the value is greater than getMaxValue().public long getGreatestPrimeLessEqual(long value)
If there are no prime numbers less or equal than the given threshold
within the internal bounds the value -1 will be returned.
If the prime values in the interval [0,value] have been computed
this method takes O(1), otherwise it takes O(N)
value - the threshold value.-1 otherwise.OutOfMemoryError - if the required memory exceeds the available one.RequirementFailure - if the value is greater than getMaxValue().public long getMaxValue()
public long getGreatestComputedValue()
public long getCurrentMemoryConsumptionInBytes()
Copyright © 2011–2020 Nerd4j. All rights reserved.