** Using Drawing panel. Implement the Sieve of Eratosthenes, which is an algorit
ID: 3621820 • Letter: #
Question
** Using Drawing panel. Implement the Sieve of Eratosthenes, which is an algorithm for finding all the prime numbers less than a given number.
The user should be asked for the value of n, the maximum value that the program will check. You should ensure that n is at least 10.
boolean Array
We will use a boolean array to indicate whether a number is marked or not. The length of the array should be n + 1. For example, if n is 10, your new array should have 11 elements
Numbers that have been marked are colored red. Numbers that have been identified as prime are colored green.
You initially set index 0 and index 1 to true, marking these two numbers as not prime (true means marked, and false means unmarked).
After the sieve algorithm is finished, the array should look like this.
index 0 1 2 3 4 5 6 7 8 9 10
value true true false false true false true false true true true
Create a boolean array of length n + 1.
Mark 0 and 1.
for (int p = 2; p * p <= n; p++)
If p is unmarked:
Initialize m to p + p.
While m <= n:
Mark m.
Add p to m.
Print all the prime numbers (the unmarked numbers).
Explanation / Answer
Dear, Here is the code import java.awt.*; import javax.swing.*; public class Sieve { public static void main( String args[] ) { int count = 0; String result = ""; JTextArea output = new JTextArea( 10, 15 ); JScrollPane scroller = new JScrollPane( output ); int array[] = new int[ 1000 ]; // initialize all array values to 1 for ( int index = 0; indexRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.