Algorithm to find all the primes less than or equal to n

Assignment Help Basic Computer Science
Reference no: EM131305062

The Sieve of Eratosthenes is an elegant algorithm for finding all of the prime numbers up to some limit n. The basic idea is to first create a list of numbers from 2 to n. The first number is removed from the list, and announced as a prime number, and all multiples of this number up to n are removed from the list. This process continues until the list is empty.

For example, if we wished to find all the primes up to 10, the list would originally contain 2, 3, 4, 5, 6, 7, 8, 9, 10. The 2 is removed and announced to be prime. Then 4, 6, 8, and 10 are removed, since they are multiples of 2. That leaves 3, 5, 7, 9. Repeating the process, 3 is announced as prime and removed, and 9 is removed because it is a multiple of 3. That leaves 5 and 7. The algorithm continues by announcing that 5 is prime and removing it from the list. Finally, 7 is announced and removed, and we're done.

Write a program that prompts a user for n and then uses the sieve algorithm to find all the primes less than or equal to n.

Reference no: EM131305062

Questions Cloud

Write and test a function removeduplicates : Write and test a function removeDuplicates(somelist) that removes duplicate values from a list.
Will it have less money available to lend : How would increasing loan loss reserves reduce the risk of bankruptcy for a smaller bank?- If a bank increases its loan loss reserves, will it have less money available to lend?
Calculate npv and irr for project-reject pineapple project : Hawaiian Coconut Milkshakes Inc. is considering the following project. They would like to expand and add a pineapple branch. The firm has paid $95,000 to a consultant in order to find out more about the available projects and possibilities. Calculate..
What is first nationals roe now : Suppose First National's equity capital declines to $10 million, while its assets and ROA are unchanged. What is First National's ROE now?
Algorithm to find all the primes less than or equal to n : The first number is removed from the list, and announced as a prime number, and all multiples of this number up to n are removed from the list. This process continues until the list is empty.
Explanation of a bank experiencing rising total profits : An article in the Wall Street Journal states that Royal Bank of Canada's ROE.- What is the most likely explanation of a bank experiencing rising total profits and a falling ROE?
.how is production planned from customer demand : Customer demand and process capacity relationships.How is production planned, from customer demand to production schedules?Quality Management issues.Product delivery issues.The interfaces of the process with both the external and internal customers.
Would you become an investor in the bank : You learn that the bank has $300 million in capital and $5 billion in assets. Would you become an investor in this bank? Briefly explain.
Identify two weaknesses target has in comparison to walmart : Identify 1-2 weaknesses Target has in comparison to Walmart. For each weakness, explain your rationale in 2-3 sentences. Identify 1-2 strengths Target has in comparison to Walmart.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Description of a probability distribution

How can mean, standard deviation, and variance assist in the description of a probability distribution?

  Test the null hypothesis that the three treatments

Test the null hypothesis that the three treatments have the same effect on bacterial concentration.

  Interest rate expected during year

If the expectations theory of the term structure is correct, what is the 1-year interest rate expected during Year 3? (Base your answer on an arithmetic rather than geometric average.)

  What is included in the full course package

What is included in the full course package. The entire course study guide includes the homework solution in notepad for DBM

  Define t-test and test a sample data

Define t-Test and test a sample data

  Electric connection between the rotating coil of wire

What is the name of the component which forms the electric connection between the rotating coil of wire and the external source of electrical energy?

  Determine the temperature of the energy source

Determine the temperature of the energy source, the amount of air contained in the engine, and the maximum air pressure during the cycle.

  Estimate the total amount of oxygen

Use technology to estimate the total amount of oxygen consumed during the24th and25th days(t=23tot=25).Round your answer to the nearest10 milliliters.

  Wired and wireless technology

Review the chart in the "Networking Technologies Used for Internet Connections" section of Chapter 16. What types of technology (wireless or wired) are used in business today? Include at least two examples. Which wireless technology types do you t..

  Define the term principal-agent problem

1) Define the term principal-agent problem. What principal-agent problems exist in a market? What principal-agent problems exist in a firm? What are possible solutions to principal-agent issues within the firm?

  Convert this cfg to one with-out the lambda-production then

convert this CFG to one with-out the lambda-Production then.

  Market system and a centrally-planned system

Explain the major differences between a free market system and a centrally-planned system.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd