1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
umka21 [38]
3 years ago
8

Prime numbers can be generated by an algorithm known as the Sieve of Eratosthenes. The algorithm for this procedure is presented

here. Write a program called primes.js that implements this algorithm. Have the program find and display all the prime numbers up to n = 150.
Sieve of Eratosthenes Algorithm
To Display All Prime Numbers Between 1 and n
Step 1: We need to start with all the numbers representing the range of numbers that are possible candidate primes. So, create an array of consecutive integers from 2 to n: (2,3,4,..n). I wouldn't hand-code this. I would use a loop to populate the array.
Step 2: At each step we select the smallest number and remove all it's multiples. So we'll start with an outer loop that goes from 2 to n. initially, let p equal 2, the first prime number.
Step 3: In an inner loop we need to iterate over all the numbers that are multiples of p, i.e for 2, that's 2,4,6,8 etc. Each time, setting it's value to false in the original array.
Step 4: Find the first number greater than p in the list that is not marked False. If there was no such number, stop. Otherwise, let p now equal this number( which is the next prime), and repeat from step 3.
When the algorithm terminates, all the numbers in the list that are not marked False are prime
Example: Let us take an example when n = 50. So we need to print all print numbers smaller than or equal to 50. We create list of all numbers from 2 to 50.
2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
Computers and Technology
1 answer:
Sidana [21]3 years ago
4 0

Answer:

const MAXNR=150;

let candidates = {};

for(let i=2; i<=MAXNR; i++) {

   candidates[i] = true;

}

for(let p=2; p <= MAXNR; p++) {

   if (candidates[p]) {

       process.stdout.write(`${p} `);

       // Now flag all multiples of p as false

       i=2;

       while(p*i <= MAXNR) {

           candidates[p*i] = false;

           i++;

       }

   }

}

You might be interested in
Write the definition of a method printDottedLine, which has no parameters and doesn't return anything. The method prints to stan
Bond [772]

Answer:

public static void printDottedLine()

{

  System.out.println(".....");

}

Explanation:

The above written code is in JAVA and it is the method definition of the method mentioned in the question.Since the method returns nothing hence it's return type is void and it does not have any arguments hence there is nothing written in the parenthesis.In JAVA System.out.println is used for standard output and trminates them by newline.

3 0
3 years ago
Which task can be completed with the Template Organizer?
spin [16.1K]

Answer:

B is the answer

Explanation:

i just got it right

8 0
3 years ago
Read 2 more answers
Provide a brief, high-level description of how the Internet’s connection-oriented service provides reliable transport.
mario62 [17]

Answer:

  Connection-oriented is the method which is implemented in the transport and data link layer. It is basically depend upon the physical connection in the network system.

Transmission control protocol (TCP) is the connection oriented and user datagram protocol (UDP) is the connection less network transport layer. The TCP and UDP both are operate in the internet protocol (IP).

It basically require connection and it can be establish before sending data. This is also known as reliable network connection service. The connection oriented service establish connection between the different connection terminal before sending any data or information. It can easily handle traffic efficiently.

7 0
3 years ago
Given the code as follows: main() { int i = 3, n; float x; x = i; n = 8 % x; } What problem will occur? Group of answer choices
Bess [88]

Answer:

A compilation error will occur.

Explanation:

The % operator does not accept a float as its right-hand operand.

7 0
3 years ago
If your computer is running slowly, which of the following is most likely to solve the problem?
sleet_krkn [62]

Answer:

Purchase Anti-virus

Explanation:

Delete old files.

clear your temp

update to a ssd

8 0
3 years ago
Other questions:
  • Sleeping is an effect of _______ , but agitation may cause a person to wake up
    7·2 answers
  • In internet terminology, what is the term, .com, called?
    12·2 answers
  • Which member of the restaurant and food/beverage service career is mostly likely to plan menus and direct worker
    6·2 answers
  • COMPUTER ORGANIZATION &amp; ARCHITECTURE I question 3<br> need correct the answers to all
    12·1 answer
  • Which of the following statements are true about quick sort
    12·1 answer
  • In the view that follows, which field can't be updated create view example_2 as select invoice_number, invoice_date, invoice_tot
    13·1 answer
  • A(n) __ is a list of main points and sub-points of a topic to include in a presentation
    14·2 answers
  • 7.5 code practice please I can't get a 100 on it helpppp :/
    6·2 answers
  • Which of the following is the most reliable way to check the accuracy of a website?
    13·1 answer
  • WHO WANT P O I N T S.................................................
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!