ProjectEuler.net problem #50, Consecutive Prime Sum I am trying to solve ProjectEuler.net problem #50, Consecutive Prime Sum. Here is the problem:

The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?

I have written some code which solve the problem just fine when the limit is 10, 100, 1000 or 10000 but when the limit is 1000000 as the problem requires, the program takes too much time to finish running!!
What can be done to my code to make the program faster?
package com.company;
import java.util.ArrayList;

class ConsecutivePrimeSum {
    public static int limit=1000000;
    int lengthOfTheLongest =0;
    int sumOfTheLongest =0;


    void solution(){
        ArrayList&lt;Integer&gt; arrayOfPrimes=generatePrimes();
        scanSequences(arrayOfPrimes);
        System.out.println(&quot;The longest sum is &quot;+ sumOfTheLongest +&quot;  and contains &quot;+lengthOfTheLongest+&quot; terms&quot;);
    }
    private ArrayList&lt;Integer&gt; generatePrimes(){
        ArrayList&lt;Integer&gt; arrayOfPrimes=new ArrayList&lt;Integer&gt;();
        for(int i=limit;i&gt;=2;i--){
            if(isPrime(i)){
                arrayOfPrimes.add(i);
            }
        }
        return arrayOfPrimes;
    }

    /**
     * @param s
     * this scans ArrayList s, to get sequence of prime numbers and calculate
     * their sum and corresponding length
     * then assign the longest length to the variable lengthOfTheLongest, and its sum to variable sumOfTheLongest
     */
    private void scanSequences(ArrayList&lt;Integer&gt; s){
        ArrayList&lt;Integer&gt; cumulativeSums=generateCumulativeSums(s);
        for(int start=1;start&lt;=s.size();start++){
            for (int end=s.size()-1;end&gt;=start;end--){
                if(!isPrime(sumFromTo(cumulativeSums,start,end)))  continue;
                if(sumFromTo(cumulativeSums,start,end)&gt;limit) break;
                if ((end-start+1)&gt; lengthOfTheLongest){
                    sumOfTheLongest =sumFromTo(cumulativeSums,start,end);
                    lengthOfTheLongest =(end-start+1);
                }
            }
        }

    }

    /**
     * @param s
     * @return arraylist of cumulative sums of the elements in s
     */
    private ArrayList&lt;Integer&gt; generateCumulativeSums(ArrayList&lt;Integer&gt; s){
        int sum=0;
        ArrayList&lt;Integer&gt; cumulativeSums=new ArrayList&lt;Integer&gt;();
        for (int i=0;i&lt;s.size();i++){
            cumulativeSums.add(sum=sum+s.get(i));
        }
        return cumulativeSums;
    }

    /**
     * @param a is an ArrayList whose elements are to be summed
     * @param start is the index of where summing should start
     * @param end is the index of where summing should end
     * @return the obtained sum
     */
    private int sumFromTo( ArrayList&lt;Integer&gt; a,int start, int end){
        int sum;
        sum=a.get(end)-a.get(start-1);
        return sum;
    }

    private static boolean isPrime(int number){
        boolean isPrime=false;
        int divider=2;
        int count=0;
        while(divider&lt;=number){
            if(number%divider==0)
                count=count+1;
            divider=divider+1;
        }
        if(count==1)
            isPrime=true;

        return isPrime;
    }


}
class Test{
    public static void main(String [] args){
        ConsecutivePrimeSum consecutivePrimeSum=new ConsecutivePrimeSum();
        consecutivePrimeSum.solution();
    }
}

```
Generation of prime numbers is suboptimal. Use a sieve of Erathosthenes.
isPrime is highly suboptimal. You already generated an array of all necessary primes, so just binary search it.
Breaking the loop in

        if(sumFromTo(cumulativeSums,start,end)&gt;limit)
            break;


looks like a bug. The intention is to loop by decreasing end, yet since the loop starts with the very long sequence, its sum is likely to exceed the limit right away. Shorter sequences with the same start are never tested.

Consider finding the proper end as an upper limit of a sumFromTo(cumulativeSums, start, end) &lt;= limit predicate (hint: another binary search).
You are not interested in all sequences of the primes. Most of the primes execs 2 are odd. Notice that if the sequence has an even number of odd primes, its sum is even, that is not a prime. Once the correct end is established, you may safely decrease it by 2.
Style wise, give your operators some breathing space.

    for (int end = s.size() - 1; end &gt;= start; end--) {


is much more readable than

    for (int end=s.size()-1;end&gt;=start;end--){



PS: I am not aware of any math regarding sums of consecutive primes. It is very much possible that such math exists, and the goal of this problem is to discover it. That would be a true optimization.