Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008, 08:13 PM
Member
 
Join Date: Feb 2008
Posts: 8
perito is on a distinguished road
Math Related Problem
Im trying to solve this
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?
I wrote this code, it should work but it doesnt !
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?
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 the value of maxValue to 100 and see the correct answer,
change it to 1000 and see it go all wrong... WHY?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-21-2008, 10:53 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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:
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
Here's a different test showing what sum is at each output:
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
Since your code requires consecutive sums, you may wish to try re-initializing i once it's reached 1000 and the correct answer is still not found(how you do this I leave for you- maybe an outer surrounding loop?). For example, you start off with int i = 2; and since the sum of the above output is actually 963, it lead me to think you're only ten off... thus what would happen if i was started or restarted off at 7 instead of 2? It would be ten subtracted- hence, 953. Example, for int i = 7; we get:
Code:
7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 = 953
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math Related Problem perito New To Java 3 03-20-2008 07:22 PM
Issue related to browser sachindanayak Java Servlet 0 02-03-2008 04:25 AM
Exceptions related to DynaValidatorForm rameshraj JavaServer Pages (JSP) and JSTL 1 12-27-2007 01:44 PM
Exceptions related to DynaValidatorForm rameshraj Java Servlet 0 12-26-2007 12:43 PM
Bean related actions in JSP Java Tip Java Tips 0 12-24-2007 12:04 PM


All times are GMT +3. The time now is 02:15 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org