• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sieve of Eratosthenes using a set  RSS feed

 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm given the following problem:

Print prime #'s between 2 - n using this algorithm:
- Insert ALL #'s between 2 - n into set.
- Remove multiples of 2 from set, except 2.
- Remove multiples of 3 from set, except 3.
- Continue process till square root of n.
- Print the set.

I've been stuck for the last couple of hours after simply getting all the #'s between 2 - n into the set and was hoping you guys can help me out in understanding this.
This is what I currently have, which isn't much:


I appreciate any help in the right direction!
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've started well.  Now, how to remove multiples of two.  Have you tried just doing multiples of two and not worrying about the other multiples?  This sometime helps to get your brain going in the right direction.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what are you stuck on? You have done the "Insert ALL #'s between 2 - n into set" part, what's keeping you from trying to do the "Remove multiples of 2 from set, except 2" part?
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You've started well.  Now, how to remove multiples of two.  Have you tried just doing multiples of two and not worrying about the other multiples?  This sometime helps to get your brain going in the right direction.

Junilu Lacar wrote:So what are you stuck on? You have done the "Insert ALL #'s between 2 - n into set" part, what's keeping you from trying to do the "Remove multiples of 2 from set, except 2" part?


Thanks for the quick reply.
The problem that I run in to is how to remove the multiples of two while using the iterator to iterate through the set.
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's amazing what a simple post, and a few replies on these forums can do.
So this is where I'm currently at:
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good step forward. I must warn you in advance that your instructions are flawed.  The part that says "Continue process till square root of n" is misleading and open to interpretation. To prove my point, tell us what you think that instruction means. I'd bet a beer it's going to be misguided.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things to nitpick about your last code listing posted:

1. You really should align your braces properly. Misaligned braces make your code difficult to read and misleading. Code that's hard to read can lead to bugs. Code that's misleading attracts bugs. The closing brace on line 17 should be aligned with the closing brace on line 12. The closing brace on line 19 should have the same indentation level as the public keyword on line 8 and line 21.

2. Be generous with your white spaces. They make your code easier to read. Put blank lines around the System.out.println() statements on lines 13 and 18. This makes it easier to make out the different sections of your code. This will help you later when you get better at making your methods smaller. You want to keep your methods small and focused on doing one thing. And no, your getPrimes() method is not just doing one thing. Don't worry about it for now; just keep that in mind for later. For now, just start making it a habit to put some visual separation between sections of code in your methods.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!