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
E-mail is the most common distributed application that is widely used across all architectures and vendor platforms.a) trueb) fa
Advocard [28]

Answer:

A. True.

Explanation:

E-mail is an acronym for electronic mail and it can be defined as an exchange or transmission of computer-based data (messages) from one user to another over a communications network system.

Also, a distributed application refers to a software program that is capable of running on several computer systems and can communicate effectively through a network.

E-mail is the most common distributed application that is widely used across all architectures and vendor platforms because it primarily operates on a client-server model, by providing users with the requested services through the Simple Mail Transfer Protocol (SMTP) using the standard port number of 25.

7 0
3 years ago
In portrait mode, your camera will use what?
vesna_86 [32]

Answer:

A. A Smaller Aperture

Have A Good Day

3 0
3 years ago
In two to four sentences describe how you would center text?
elena-14-01-66 [18.8K]
You can highlight all of the text you want to center. The next step is to go to the center function in the paragraph tab under the home tab. Or you could just use the keys CTRL and E to center the words highlighted.
4 0
3 years ago
Read 2 more answers
(10 points) [Edhesive] 3.6 Code Practice
Digiron [165]

Answer:

  • You need to create a variable outside (before) the input loop.
  • You need a variable inside your loop that temporarily holds the user input.
  • In your loop, you will compare if the variable outside the loop is greater than (or less than) the new user input.

I don't know what program language you are using, but I will use python since it's easy to read and you will get the idea if you're using a different programming language.

<u>Code (Python)</u>

largestnum = 0

for x in range(6): <em>#loops 6 times</em>

   newnum = int(input("Enter a number: ")) #ask user for input & converts to int.

   if newnum > largestnum: <em>#if new number is greater than largest num </em>

     largestnum = newnum     <em>#make it the largest number</em>

<em>    </em>print("Largest:", largestnum)

The reason why you need a variable outside of the loop is because after the loop is done all variables inside the loop are trashed and can no longer be accessed outside of the loop.

3 0
4 years ago
What operating system allows various teams in its office to network and collaborate on projects
Nataliya [291]
It depends what youre doing. If youre doing a collab project you could use mac, windows, or linux. But the basis and best option is Windows Professional 64bit.
5 0
3 years ago
Other questions:
  • You buy a $3,500 car and finance it through the car dealer. the contract says if you are two months delinquent with your payment
    9·1 answer
  • 1. Customizable diagrams, including List, Process, and Cycle diagrams, are built into Word and can be found in
    15·2 answers
  • Intro to Computers quiz answers me need for test please help me hurry
    5·1 answer
  • Google Ads was designed to help businesses achieve online success. To accomplish this, Google Ads was built on three core princi
    13·1 answer
  • The drawback to copyright protection is that the underlying ideas behind the work are not protected, only their manifestation in
    9·1 answer
  • Which of the following commands appears in the INSERT menu or tab?
    10·1 answer
  • What is a common misconception about Agile and DevOps?
    9·1 answer
  • Write a Boolean expression that tests if the value stored in the variable num1 is equal to the value stored in the variable num2
    10·1 answer
  • An international pharmaceutical company is fully compliant with local and international regulations. However, they suffered a ma
    14·1 answer
  • Dentify the benefits of workplace diversity. Select all that apply.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!