On 04/28/2014 07:53 PM, Brian Burkhalter wrote:

Meanwhile I suppose that I could convert my test file and use the 
sun.misc.UU{En,De}coder classes to handle it in the test.

Uh-oh, we have java.util.Base64 these days.

But in this case, it's probably simpler to use this code snippet instead of adding 235 KiB of incompressible binary data to the source code.

    private static boolean[] primes(int n) {
        boolean[] primes = new boolean[n + 1];
        Arrays.fill(primes, true);
        primes[0] = false;
        primes[1] = false;
        for (int p = 2; p * p < n; ) {
            for (int i = 2 * p; i < primes.length; i += p)
                primes[i] = false;
            do
                ++p;
            while (!primes[p]);
        }
        return primes;
    }

(I hope it's correct, but you get the idea…)

Anyway, for the first 100K primes, it runs in roughly 400 ms on my machine, including JVM startup and teardown. That should be completely negligible compared to actually testing all the numbers for primality using a any non-sieve algorithm.

--
Florian Weimer / Red Hat Product Security Team

Reply via email to