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<Integer> arrayOfPrimes=generatePrimes();
scanSequences(arrayOfPrimes);
System.out.println("The longest sum is "+ sumOfTheLongest +" and contains "+lengthOfTheLongest+" terms");
}
private ArrayList<Integer> generatePrimes(){
ArrayList<Integer> arrayOfPrimes=new ArrayList<Integer>();
for(int i=limit;i>=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<Integer> s){
ArrayList<Integer> cumulativeSums=generateCumulativeSums(s);
for(int start=1;start<=s.size();start++){
for (int end=s.size()-1;end>=start;end--){
if(!isPrime(sumFromTo(cumulativeSums,start,end))) continue;
if(sumFromTo(cumulativeSums,start,end)>limit) break;
if ((end-start+1)> lengthOfTheLongest){
sumOfTheLongest =sumFromTo(cumulativeSums,start,end);
lengthOfTheLongest =(end-start+1);
}
}
}
}
/**
* @param s
* @return arraylist of cumulative sums of the elements in s
*/
private ArrayList<Integer> generateCumulativeSums(ArrayList<Integer> s){
int sum=0;
ArrayList<Integer> cumulativeSums=new ArrayList<Integer>();
for (int i=0;i<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<Integer> 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<=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();
}
}
Code Review aims to help improve working code. If you are trying to figure out why your program crashes or produces a wrong result, ask on Stack Overflow instead. Code Review is also not the place to ask for implementing new features.
\$\endgroup\$