hanseljosephfernandezdw
New Member
I am attempting Problem 50 of project euler.\[quote\] 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?\[/quote\]Here is my code:\[code\] public class consPrime { static int checker(int ar[],int num,int index) //returns no.of consecutive { //primes for the given num while(true) { int temp=num; for(int i=index;i>=0;i--) { temp=temp-ar; if(temp==0) { return (index-i+1); } } index--; if(index==0) return 0; } } public static void main(String args[]) { int n=100000; int ar[]=new int[n]; int total=0;int flag; for(int i=2;i<1000000;i++) //Generates an array of primes below 1 million { flag=1; for(int j=2;j<=Math.sqrt(i);j++) { if(i%j==0) { flag=0; break; } } if(flag==1) { ar[total]=i; total++; } } int m=0; int Big=0; for(int i=total;i>=0;i--) //Prints the current answer with no.of prime { m=checker(ar,ar,i-1); if(Big<=m) {Big=m; System.out.println(ar+" "+Big); } } }}\[/code\]Basically it just creates a vector of all primes up to 1000000 and then loops through them finding the right answer. The answer is 997651 and the count is supposed to be 543 but my program outputs 990707 and 75175 respectively. What might be wrong?