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]
2 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]2 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
A ________ is s field or set of fields in a record that uniquely defines the record.
Archy [21]
A primary key is the answer. =)
5 0
3 years ago
Dam naj za pierwszej osobie która odpowie dobrze!
Rama09 [41]
I don’t understand that language
8 0
2 years ago
In what country was the English royal family and nobility living before the Restoration?
krek1111 [17]

im not sure to be exact , but i think its england. i have done an assignment in the past and that was the awnser.....hope i help                      

4 0
3 years ago
Sharon is responsible for the security on web applications. She’s looking to see if all applications have input validation. What
LiRa [457]

Answer:

Options Include:

<em>A) Server-side validation </em>

<em>B) Client-side validation </em>

<em>C) Validate in trust </em>

D) Client-side and server-side validation

<em>Client-side and server-side validation is Correct</em>

Explanation:

The best option is to validate the client side with the server side. Using these together would provide the best testing option for Sharon.

<em>This keeps user feedback instantly without wasting postbacks while also protecting against JavaScript disabled users. That's how the validation controls for ASP.NET operate. </em>

This is definitely not over-engineering as there are risks of using one without the other.

Individual validation on the server side and individual validation on the client side are both incorrect. Trust validation is not a form of validation.

4 0
3 years ago
What are 3 possible causes of getting a "network cable unplugged" message on a pc, assuming that the cable is not actually unplu
Nat2105 [25]
I've had that problem before. The cause of my problem was 2 things and that was the age of the cable, and build up of dust on each of the cable.
7 0
3 years ago
Other questions:
  • Analyze the following code.
    12·1 answer
  • Assume the following JavaScript program was interpreted using staticscoping rules. What value of x is displayed in function sub1
    12·1 answer
  • Why is it not advisable to mark tlc plates with a pen to indicate?
    12·1 answer
  • Timeliness is an important goal of any access control monitoring system.<br> A. True<br> B. False
    9·1 answer
  • Which of the following statements is correct?
    11·1 answer
  • What is the correct order of headers, from left to right, in a completed frame?
    8·1 answer
  • When the "swen" virus infected someone's system, it made significant changes to the registry that caused it to be extremely diff
    11·1 answer
  • The default print setting for worksheets is________
    13·1 answer
  • Who ever can get me the lyrics to raining tacos will get 46 points + the crown! i want the song!
    10·2 answers
  • I need help on this, need answer asap.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!