public final class Primes extends AbstractList<Integer> implements java.util.function.IntFunction<Integer>, RandomAccess
| Modifier and Type | Class and Description |
|---|---|
static class |
Primes.InitializationException
Thrown if a Primes object can not be constructed, for instance because it
was attempted to load a given number of primes form a file but an IOException
occurred, or the file was malformed, yada yada yada.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
NUMBER_OF_PRIMES_UPTO_INT_MAX_VALUE
The number of primes in the interval
[ 2 .. |
modCount| Modifier and Type | Method and Description |
|---|---|
Integer |
apply(int value) |
boolean |
contains(Object n) |
static Primes |
generate()
Generates all the primes upto
Integer.MAX_VALUE (i.e. |
static Primes |
generate(int howMany)
Generates all the primes upto the specified limit of
howMany. |
static int[] |
generate(int howMany,
java.util.function.IntConsumer handler)
Generates all the primes up to
howMany (specialised for int). |
static long[] |
generate(int howMany,
java.util.function.LongConsumer handler)
Generates all the primes up to
howMany (specialised for long). |
Integer |
get(int index)
Returns the nth prime, where the prime with index 0 is 2.
|
List<Integer> |
getPrimeFactors(int n) |
void |
getPrimeFactors(int n,
java.util.function.IntConsumer handler)
Enumerates the prime factors of a given integer
n. |
NavigableSet<Integer> |
getPrimeFactorsSet(int n) |
int[] |
getUnderlyingArray()
Exposes the underlying array.
|
boolean |
isPrime(int n)
Checks whether the given int is prime, by checking whether it is contained
in the list of primes in this instance.
|
static int |
isqrt(int n)
Integer square root of an int.
|
static long |
isqrt(long n)
Integer square root of a long.
|
Iterator<Integer> |
iterator() |
static Primes |
load()
Loads all the available primes that are shipped with this library.
|
static Primes |
load(int howMany)
Loads the specified amount of primes that are shipped with this library.
|
int |
size()
Returns the number of primes in this instance.
|
add, add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subListaddAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitaddAll, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArrayparallelStream, removeIf, streampublic static final int NUMBER_OF_PRIMES_UPTO_INT_MAX_VALUE
[ 2 .. Integer.MAX_VALUE ] (including 2 and
Integer.MAX_VALUE which itself is a prime number).public static long isqrt(long n)
The result is guaranteed to be less than or equal to the actual square root, and the result incremented by one is guaranteed to be greater than the actual square root; i.e. the result is the actual square root rounded down.
n - The integer to take the square root of.public static int isqrt(int n)
The result is guaranteed to be less than or equal to the actual square root, and the result incremented by one is guaranteed to be greater than the actual square root; i.e. the result is the actual square root rounded down.
n - The integer to take the square root of.public static Primes load()
[ 2 .. Integer.MAX_VALUE ]public static Primes load(int howMany)
howMany - How many primes to load from the library resources.public static Primes generate(int howMany)
howMany.howMany - How many primes to generate.howMany primes starting from 2.IllegalArgumentException - Throws an exception if howMany exceeds
NUMBER_OF_PRIMES_UPTO_INT_MAX_VALUE,
as primes beyond this threshold exceed Integer.MAX_VALUE.public static Primes generate()
Integer.MAX_VALUE (i.e. all the primes that
fit into the int datatype.int.public static long[] generate(int howMany,
java.util.function.LongConsumer handler)
howMany (specialised for long).
This method uses am modified version of the sieve of eratosthenes to calculate the primes, hence it allocates an int-array of howMany primes upfront.
The fully populated array is returned in the end, but every time a new prime number
is found the handler is invoked with that prime number (which is
useful to stream it info a file for example).
howMany - How many prime numbers to calculate.handler - A handler to invoke every time a prime number is found.public static int[] generate(int howMany,
java.util.function.IntConsumer handler)
howMany (specialised for int).
This method uses am modified version of the sieve of eratosthenes to calculate the primes, hence it allocates an int-array of howMany primes upfront.
The fully populated array is returned in the end, but every time a new prime number
is found the handler is invoked with that prime number (which is
useful to stream it info a file for example).
howMany - How many prime numbers to calculate.handler - A handler to invoke every time a prime number is found.IllegalArgumentException - Throws an exception if howMany exceeds
NUMBER_OF_PRIMES_UPTO_INT_MAX_VALUE,
as primes beyond this threshold exceed Integer.MAX_VALUE.public int size()
size in interface Collection<Integer>size in interface List<Integer>size in class AbstractCollection<Integer>public int[] getUnderlyingArray()
This array is mutable, do not touch it.
public Integer get(int index)
public Integer apply(int value)
apply in interface java.util.function.IntFunction<Integer>public boolean isPrime(int n)
Note: If you loaded less primes than there are in the range of values for int, then this method might return false for an integer that actually is prime; simply as it was not loaded.
n - The integer to check.public boolean contains(Object n)
contains in interface Collection<Integer>contains in interface List<Integer>contains in class AbstractCollection<Integer>public void getPrimeFactors(int n,
java.util.function.IntConsumer handler)
n.n - The integer to compute the prime factors for.handler - A callback handling each prime factor found.public NavigableSet<Integer> getPrimeFactorsSet(int n)
Copyright © 2018. All rights reserved.