Results 1 to 5 of 5
 01112014, 12:45 AM #1Señor Member
 Join Date
 Jan 2014
 Posts
 184
 Rep Power
 0
Efficient way to find Primes and sums of primes
Java Code:class Program { public static void main (String[] args) { long max = 200; //add up all primes up to 200 long ans = 0; for(long x = 2; x < max; x++) { if(isPrime(x)) { ans += x; } } System.out.println("Answer is: " + ans); } public static boolean isPrime(long x) { for(long z = 2; z < x; z++) { if(x % z == 0){ return false; } } return true; } }
BEFORE YOU ANSWER: Don't point me to the sieve of Eratosthenes, if that's what you are about to post  I'm not quite sure how to implement that kind of method into my program, so if you know, please explain and give me an example. Thanks
 01112014, 12:59 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,299
 Rep Power
 9
Re: Efficient way to find Primes and sums of primes
You don't need to the sieve to find the primes. But what you can do is store each prime you find and then only divide by those up to the square root of the target. It is still a trial by division method so it is suboptimal to an actual sieve. More information is here > Trial division  Wikipedia, the free encyclopedia. And don't forget to check out the reference articles.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01112014, 08:20 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,292
 Blog Entries
 7
 Rep Power
 24
 01112014, 05:50 PM #4Señor Member
 Join Date
 Jan 2014
 Posts
 184
 Rep Power
 0
Re: Efficient way to find Primes and sums of primes
Hmmmm, if only looking at the first half of the sentance, I guess that's what you'd get out of it. But the second half states what I was trying to say  aka, don't just tell me that it exists, because I already know about it. I'd much rather see how to implement it as I tried to and couldn't.
Well, I guess since you answered and know about it, you can confirm or deny the way I was thinking about it.
I was going to make an arraylist with all of the possible integers from 2 to 2000000, then I would start with the first integer, loopthrough the arraylist and remove anything that is evenly divisible by 2. Then move on to 3 and continue. Is that correct?
And thanks Jim for the helpful reply :)
 01112014, 06:31 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,292
 Blog Entries
 7
 Rep Power
 24
Similar Threads

More efficient way to find the path of all folders with a specific name
By ralph in forum SWT / JFaceReplies: 8Last Post: 08272013, 08:50 PM 
Sum of primes have some trouble!
By banglc11 in forum Threads and SynchronizationReplies: 9Last Post: 04272012, 10:05 PM 
2d Array Sums Help
By XxVashxX in forum New To JavaReplies: 3Last Post: 10172011, 06:20 PM 
Harmonic sums  Recursive
By überfuzz in forum New To JavaReplies: 4Last Post: 03252011, 11:42 AM 
Problem with Sums and Averages in Sales Report
By DavidEvans in forum New To JavaReplies: 9Last Post: 04212010, 08:57 PM
Bookmarks