Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. . Answer the questions regarding the following pro- gram import java.util.Arr

ID: 3574176 • Letter: 3

Question

3. . Answer the questions regarding the following pro- gram

import java.util.ArrayList;

public class Eratosthenes {

   public static void main(String []args) {

   ArrayList<Integer> al = new ArrayList<>();

   int n = 120;

   for(int i = 0; i < n; i++) {

al.add(1); }

   al.set(0,0);

   al.set(1,0);

   for(int i = 2; i < n; i++) {

   for(int j = 2; i*j < n; j++) {

   al.set(i*j,0);

} }

   for(int i = 0; i < n; i++) {

   if (al.get(i) > 0) {

   System.out.println(i);

   }

} }

}

b : What are the common characteristics of the printed numbers. Explain brie y. ?

c : The following program execute the same algorithm as the above. Complete the following program.

import java.util.ArrayList;

public class Prime {

public static void main(String []args) {

ArrayList<Object> al = new ArrayList<>();

int n = 120;

for(int i = 0; i < n; i++) {

_________; }

___________;

___________;

for(int i = 2; i < n; i++) {

for(int j = 2; i*j < n; j++) {

_____________;

} }

for(int i = 0; i < n; i++) {

if (______________________) {

System.out.println(i);

}

} }

}

Explanation / Answer

answer a> The output of the code is :
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113


answer b> the numbers in the list are prime numbers. The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so.

answer c> In order to find Prime numbers below is the code :

import java.util.ArrayList;
public class Prime {
public static void main(String []args) {
ArrayList<Object> al = new ArrayList<>();
int n = 120;
for(int i = 0; i < n; i++) {
       __al.add(1)__; }
___al.set(0,0)__;
___al.set(1,0)___;
for(int i = 2; i < n; i++) {
for(int j = 2; i*j < n; j++) {
_____________;
} }
for(int i = 0; i < n; i++) {
if (_____al.get(i) > 0_____) {
System.out.println(i);
}
} }
}

feel free to ask if you have any doubt :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote