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
Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-th
pshichka [43]

The best scenario is when a program suffers from frequent page faults. In the situations when a kernel thread experiences a page fault, another kernel thread can be switched in; something a single-threaded process will not be capable of doing. Another best circumstance is when a program has to wait for other systems events.






5 0
3 years ago
How do I delete my Brainly account?
Triss [41]

Answer:

yeah how? please tell me

Explanation:

5 0
2 years ago
Read 2 more answers
The AutoRecovery feature would be useful for which situations? Check all that apply.
Aliun [14]

Answer:

All of the above but I'm not 100% sure.

5 0
3 years ago
Code.org lesson 8 level 5
Likurg_2 [28]

Answer:

and?

Explanation:

5 0
3 years ago
Read 2 more answers
When a superclass variable refers to a subclass object and a method is called on that object, the proper implementation is deter
Anni [7]

Answer:

D. Late binding

Explanation:

a. early binding.

b. non-binding.

c. on-time binding.

d. late binding.

The compiler performs a process called binding when an object is assigned to an object variable. The early binding (static binding) refers to compile time binding and late binding (dynamic binding) refers to runtime binding. Another name for late binding is dynamic linkages

It is a computer programming mechanism in which the method being called upon an object or the function being called with arguments is looked up by name at runtime.

When a superclass variable refers to a subclass object and a method is called on that object, the proper implementation is determined at execution time. The process of determining the correct method to call is known as Late Binding.

7 0
3 years ago
Read 2 more answers
Other questions:
  • Jail and prison officials may generally limit inmate rights when the limitations serve
    13·2 answers
  • You can drag a cell to a new location by pointing to the cell border until the pointer displays a _______ arrow, and then draggi
    9·2 answers
  • Why would you set up a workbook to be shared if you are the only one using the workbook?
    7·1 answer
  • You are having problems on your Windows 7 computer and you pull up Device Manager to see if there are any alerts. Two of your de
    11·1 answer
  • What kind of printers tend to have problems with ink drying out inside the nozzles when the printer is not used for a period?
    15·1 answer
  • In addition to using comments and track changes, you can also use the comparison feature for reviewing documents. In this activi
    11·1 answer
  • Submit your 400-word essay that analyzes and evaluates three different examples of digital media. need some help
    14·1 answer
  • Under the Home tab, controls for aligning text and objects can be found in the
    5·2 answers
  • Describe psychographic differences among the past five generations of Americans that you learned about in this course. What type
    7·1 answer
  • If you want to share information with individuals who are internal to your organization, which type of network would you want to
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!