hi i am having a problem to fined a prime number between 1 to 1000
Printable View
hi i am having a problem to fined a prime number between 1 to 1000
Can you explain what problems you are having? Post the code you are having problems with.
What does using an arraylist have to do with the problem?
Implement the sieve of Erathostenes: a method for computing prime numbers, known to the ancient Greeks. This method will compute all prime numbers up to n. For your implementation, choose an n of 1000. Here is how you do it:
First insert all numbers from 2 to n into a set.
Erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12...n
Erase all multiples of 3: that is, 6, 9, 10, 12....n
Repeat for all multiples up to the square root of n.
Print the numbers remaining in the set -- these are the prime numbers
You'll want to look up the methods on Set in the javadoc. Think carefully of what type of Set you will use -- is a TreeSet or a HashSet more appropriate?