Sum of maximum primes as another prime below a million
I've just switch my code from List&lt;Integer&gt; to int[] and the time went down from 19 to 11 seconds. This can be done using a loop or more simply with Guava's Ints.toArray().

While unboxing is cheap, you're needing it in the innermost loop. Which brings us to the point that there's no need for 3 nested loops, you're doing it right and I'm not. My code is faster, possibly because of my helper being faster...

public static int get50() {
    int max = 21;
    int prime_max = 953;
    ArrayList&lt;Integer&gt; primes = Helper.getPrimes(1000000);
    for (int len = 21; len &lt;= primes.size(); len += 2) {


Use some constants... all those magic numbers in code are a pain. Btw., optimizing via the given values 21 and 953 saves about nothing.

Sometimes, a loop with decreasing len could be faster, but not here as the resulting length is close to the beginning.

    if (primes.get(len - 1) &gt; 1E6) {


Again, constants please.

You could simplify the code by storing the partial sums and compute p[i] + p[i+1] + ... + p[j] simply as sum[j+1] - sum[i].

        if (primes.get(len + k - 1) &gt; 1E6 || sum &gt; 1E6) {


The first condition can never be true.

        if (Helper.isPrime((int) sum)) {
            if (len &gt; max) {
                max = len;
                prime_max = (int) sum;
            }
            break;
        }


I'm not sure about the break, but I guess that's because of the resulting sum being unique for a given length.


  If possible &amp;&amp; If reduces time Then using the new Java 8 methods.This would help me learn it too.


I strongly doubt, using Java 8 features will help, though using Java 8 surely can, because of the new optimizations.

I did some similar optimizations to my code now and the resulting time is 440 ms, where the initialization of my prime helper takes 438 ms (it always sieves up to 128 million and it's hard to change).



As we don't know why is my code faster, I'm adding my loop body taking just a few milliseconds:

for (int len=1; len&lt;primes.length; ++len) {
    for (int i=0; i&lt;primes.length-len; ++i) {
        final int j = i+len;
        if (i&gt;0 &amp;&amp; (len&amp;1) == 0) break;
        final long sum = sums[j] - sums[i];
        if (sum&gt;limit) break;
        if (!helper.isPrime(sum)) continue;
        best = len;
        result = sum;
        break;
    }
}