Results 1 to 2 of 2
Thread: Math Related Problem
 03202008, 06:13 PM #1Member
 Join Date
 Feb 2008
 Posts
 9
 Rep Power
 0
Math Related Problem
Im trying to solve this
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 onehundred.
The longest sum of consecutive primes below onethousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below onemillion, can be written as the sum of the most consecutive primes?
It works correctly for number under 100, but it doesnt even work for numbers under 1000
it gives a wrong answer 281 (it must be 953)
can anyone tell me where im going wrong?
Java Code:public class ConescutivePrimes { public static boolean isPrime(int nbr) { boolean isNPrime=true; for (int i = 2; i < nbr; i++) if ((nbr != i) && (nbr % i == 0)) { isNPrime = false; break; } if (isNPrime == true) return true; else return false; } public static void main(String[] args) { int maxValue=1000,sum=0,lastPrimeSum=0; for (int i=2;i<maxValue;i++) { if (isPrime(i) == true && sum+i<maxValue) { sum+=i; if (isPrime(sum)==true) lastPrimeSum=sum; } } System.out.println(lastPrimeSum); } }
change it to 1000 and see it go all wrong... WHY?
 03212008, 08:53 AM #2
Hi perito. I haven't had time to correct your code, but I ran a simple print test... 281 IS the last prime that your code finds  therefore it is output. You do not implement a check to correctly sum up the numbers. Here's the output from my print test:
Java Code:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 = 281
Java Code:2(2) 3(5) 5(10) 7(17) 11(28) 13(41) 17(58) 19(77) 23(100) 29(129) 31(160) 37(197) 41(238) 43(281) 47(328) 53(381) 59(440) 61(501) 67(568) 71(639) 73(712) 79(791) 83(874) 89(963) = 281
Java Code:7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 = 953
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
Similar Threads

Math Related Problem
By perito in forum New To JavaReplies: 3Last Post: 03202008, 05:22 PM 
Issue related to browser
By sachindanayak in forum Java ServletReplies: 0Last Post: 02032008, 02:25 AM 
Exceptions related to DynaValidatorForm
By rameshraj in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 12272007, 11:44 AM 
Exceptions related to DynaValidatorForm
By rameshraj in forum Java ServletReplies: 0Last Post: 12262007, 10:43 AM 
Bean related actions in JSP
By Java Tip in forum Java TipReplies: 0Last Post: 12242007, 10:04 AM
Bookmarks