Class AutoExpandingPrimesArray

  • All Implemented Interfaces:
    SieveCallback

    public class AutoExpandingPrimesArray
    extends Object
    implements SieveCallback
    An auto-expanding facade for the segmented sieve of Eratosthenes. Singleton implementation to avoid spending too much memory on the primes in different instances.
    Author:
    Tilman Neumann
    • Constructor Detail

      • AutoExpandingPrimesArray

        public AutoExpandingPrimesArray()
    • Method Detail

      • ensurePrimeCount

        public AutoExpandingPrimesArray ensurePrimeCount​(int desiredCount)
        Ensures that the array contains at least the first 'desiredCount' primes.
        Parameters:
        desiredCount -
        Returns:
        PrimeGenerator
      • ensureLimit

        public AutoExpandingPrimesArray ensureLimit​(int x)
        Ensures that the array contains all primes <= x.
        Parameters:
        x -
        Returns:
        PrimeGenerator
      • getInsertPosition

        public int getInsertPosition​(int x)
        Parameters:
        x -
        Returns:
        the index where x would be inserted into the prime array.
      • getPrime

        public int getPrime​(int n)
        Get the n.th prime, e.g. p[0]=2. This method is auto-expanding the prime array when required. This should be slower than "unsafe" raw array access, but in typical applications like trial division I could not spot any performance penalty at all.
        Parameters:
        n -
        Returns:
        n.th prime, where n starts at 0, e.g. p[0] = 2
      • processPrime

        public void processPrime​(long prime)
        Fallback method: Receives new primes from the sieve and stores them in the array.
        Specified by:
        processPrime in interface SieveCallback