Question: (Write and test a JAVA program that implements the Sieve of Eratosthenes. ) 13. A prime number is a natural number that has exactly two
(Write and test a JAVA program that implements the Sieve of Eratosthenes. )
13. A prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself.
To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
a. Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
b. Initially, let p equal 2, the smallest prime number.
c. Enumerate the multiples of p by counting to n from 2p in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ...; the p itself should not be marked).
d. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.
e. When the algorithm terminates, the numbers remaining not marked in the list are all the primes below n.
The main idea here is that every value given to p will be prime, because if it were composite it would be marked as a multiple of some other, smaller prime. Note that some of the numbers may be marked more than once (e.g., 15 will be marked both for 3 and 5).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
