calcPrimeNumbers(28) would output

The primes less than or equal to 28 are:

2 3 5 7 11 13 17 19 23

I have tried to implement methods I found into my problem but they don't work. Most of the methods I found use two methods to solve or just use a boolean to return if the value passed is prime or not.

Here is what I have so far, thank you very much for any help offered, it is greatly appreciated.

If you find a divisor, then obviously, it isn't a prime... and yes, you need to check the next count. However, just incrementing the count isn't going to do it, you need to also jump to the next iteration of the while loop, in order to process the count.

If you don't find a divisor, with one iteration of the inner loop, that doesn't mean that it is a prime -- as you do in your condition. You need to fail the test for all iterations of the inner loop before it is considered a prime.

Henry

What i'm assuming is that if the no. provided is greater than 1 than '2' itseld will be stored in a list to be displayed later to the user as a series of prime and if the no. given is either 0 or 1 then the list is null and hence no prime nos exists.

Following are the code:-

I got stuck in the logic. Right now i'm passing 10 in the construtor so the looping will be done as

2,3,4,5,6,7,8,9,10

The prime nos in the series are

2,3,5,7

But please give me some hint how can i check in the

**if**condition while iterating in

**for loop**that the current index value is a prime or not and then put it in the list.

Thanks alot.

Vinod Kumar Nair

"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."

http://www.coderanch.com/t/516856/java/java/Sieve-Eratosthenes#2343984

Everyday in every way, we get a little better.

said by BobbyYou will be interested in reading through this past forum thread as well.

http://www.coderanch.com/t/516856/java/java/Sieve-Eratosthenes#2343984

Just provide in your simple language. Don't post such complex links.

Vinod Kumar Nair

"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."

Since there was a lot more than only Sieve of Eratosthenes in that discussion (I remember it well ), it is worth doing a search of javaRanch.Bobby Smallman wrote:. . . past forum thread as well. . . .

And if your end goal was providing a list of prime numbers and not just determining if a number was prime, you can clearly use that isPrime method that way as well. I got in the habit of breaking out small chunks of code like this for Project Euler.

The Sieve can speed things up but as you have pointed out it can be overwhelming when you are new to thinking about this kind of logic. It is important to note in the isPrime method there that it only goes to the sqrt of the number looking for divisors. That is a significant speed increase at least over iterating all the way through the number.

Everyday in every way, we get a little better.

Campbell Ritchie wrote:

The Sieve is hardly more complicated, and much more efficient; that code will run in quadratic time and looks inefficient to me.Bobby Smallman wrote:. . . A simple yet efficient way . . .

I suspect the Sieve is quadratic complexity, too, but still much faster.

The Sieve is absolutely a significantly more efficient way to go. After already suggesting they research the Sieve further and read through a past post which showed the general outline of the Sieve and some pitfalls/optimizations which are helpful there was a comment that it was complex and to keep it simple. The method in my last post is an implementation of a non-Sieve method of finding prime numbers, I posted it as a stepping stone in their logic, because it is closer to their current code, to help them build a foundation in that sort of logic in order to ultimately be able to revisit their research in the Sieve.

With that being said, hopefully it is clear to the original poster that the Sieve of Eratosthenes make prime finding happy.

Everyday in every way, we get a little better.

*Please*don't post working code examples, especially in this kind of thread; remember the adage regarding lifelong piscatorial pursuits.

Everyday in every way, we get a little better.