Unfortunately only fragments of writing

Assignment Help Basic Computer Science
Reference no: EM131716510

Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived.

The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find prime numbers within a given range..

Please note: You must use this algorithm for this assignment. No other algorithm for finding prime numbers will be accepted for this assignment, although there are plenty of other and more sophisticated algorithms available!

Here are some guidelines:

  • Create a list of 1,000 values ... all of which are set to zero (hint: use list repetition)
  • This list represents the numbers 0 to 999 (based on the indexes in the list)
  • Your goal is to set all non-prime numbers in the list to 1. You can do that by using the following algorthm:
  • Start by setting the first two values (index positions 0 and 1) to 1. This lets us record that 0 and 1 are NOT prime numbers. After this step your list should look like the following:

0

1

2

3

4

5

6

7

8

9

10

-

-

-

-

-

-

-

-

-

-

-

1

1

0

0

0

0

0

0

0

0

0

  • Now, move on to the item in position #2 of the list. This item is currently zero, meaning that the number is prime. We now need to mark all future items that are evenly divisible by two as "not prime". You can do this by reading through the entire list starting at 4 (the first multiple of 2), 2 at a time, so that every element that is read contains an index which is a multiple of 2 ... and store a 1 for that element. This will result in a list in which all of the items whose index is a multiple of 2 will have a value of 1. After this step your list should look as follows:

0

1

2

3

4

5

6

7

8

9

10

-

-

-

-

-

-

-

-

-

-

-

1

1

0

0

1

0

1

0

1

0

1

  • Now, perform the exact same process for the element with an index of 3 - there is currently a zero in postion #3, so we are going to mark all multiples of 3 (starting with 6) with a value of 1. If your program finds an element which is already 1, there is nothing further to do for the item (i.e. position 6 will already be marked with a 1 at this point - we can simply skip this element and move onto the next one in the sequence (9) and mark it with a 1)
  • Next, you can perform the exact same process on the item with an index of 4 ... however, since 4 is already set to 1, there is no need to check its multiples and your program could just move on to the next item, which has an index of 5.
  • Here is the pattern as you move from one item in the list to the next: If that item has a value of 0, perform the process. If it is already 1, skip the process. It is important that you understand why you should skip this process when the element you are considering is already 1.
  • Now, repeat this process over and over. When you have finished, you will find that there are array items which still have a value of 0 ... and those items' index numbers are the prime numbers which you are seeking.
  • Your program should print out all of the prime numbers which you have found in neatly aligned columns (with up to 10 prime numbers on every row) - this can be done by essentially iterating over the list and checking the value of each item -- if there is a zero in this position, print it out as a prime number. If there is a one in this position you can skip it since it is not prime.

Here is a "visual" example:Suppose you want to find all prime numbers between 2 and 10, inclusive (your actual homework program will really look from 2 to 1000). Your list might look like this when you start :

0

1

2

3

4

5

6

7

8

9

10

-

-

-

-

-

-

-

-

-

-

-

1

1

0

0

0

0

0

0

0

0

0

Start by looking at the element with an index of 2 (as described above). Find all of the items with an index that is a multiple of 2 and change all those items to 1. This is what you would get, because 4, 6, 8, and 10 are multiples of 2:

0

1

2

3

4

5

6

7

8

9

10

-

-

-

-

-

-

-

-

-

-

-

1

1

0

0

1

0

1

0

1

0

1

Now move on to the item with an index of 3 and find all of the items whose index is a multiple of 3. Change those items to 1. (If you find an item which is already 1, it's OK to "change" it to 1 ... It just stays 1, which is fine). This is what you get, because 6 and 9 are multiples of 3:

0

1

2

3

4

5

6

7

8

9

10

-

-

-

-

-

-

-

-

-

-

-

1

1

0

0

1

0

1

0

1

1

1

Now move on to the item with an index of 5. That item is still 0, so perform the process. That is, find all of the items in the list whose index is a multiple of 5, and change all those items to 1. In this case, nothing has changed because the item with an index of 10 was already set to 1:

0

1

2

3

4

5

6

7

8

9

10

-

-

-

-

-

-

-

-

-

-

-

1

1

0

0

1

0

1

0

1

1

1

Looking at the final list just above, note which items (starting with an index of 2), still contain the value 0. You come up with items that have an index of 2, 3, 5, and 7. These are precisely the prime numbers you are looking for.Your program should print out all of the prime numbers that you have found in neatly aligned columns (with up to 10 prime numbers on every row).Notes on Efficiency:Efficiency is important here; Hint: You do not need to perform the process described above on all items of the list, for two different reasons:

  • If an item in your list has already been set to 1, what does this tell you about the multiples of that item's index?
  • 800, for example, cannot have any multiples less than or equal to 1000. Generalize this idea to make your program much more efficient.

This program should be named as follows: LastnameFirstname_assign8_sieve.py (for example, DeenaEngel_assign8_part1.py)

Reference no: EM131716510

Questions Cloud

Discuss impact of stressor on your health : major stressor in your life, and discuss its likely impact on your health. Describe the major steps that you could take
A description of the compliance issue : A comparison of the EEOC press release to the news item. What accounts for the differences?
Program that finds the cubic volume of a rectangle : Write a program that finds the cubic volume of a rectangle. Ask the user to enter the length, width and height, in feet, of a rectangle.
Define disadvantages of using eyewitness testimony in court : the advantages or disadvantages of using eyewitness testimony in court
Unfortunately only fragments of writing : He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived.
What are the risks involved if someone does not purchase : What are the risks involved if someone does not purchase long term care insurance
How would the lord have us treat those in the out-group : supporting evidence of in-group bias and/or out-group homogeneity in your example. How would the Lord have us treat those in the out-group
Solving personal problems- applying the five-step model : Solving Personal Problems: Applying the Five-Step Model
Program should display a menu : The program should display a menu that lets the user look up a person's email address, add a new name and email address, change an existing email address.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Identifies the cost of computer

identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

  Input devices

Compare how the gestures data is generated and represented for interpretation in each of the following input devices. In your comparison, consider the data formats (radio waves, electrical signal, sound, etc.), device drivers, operating systems suppo..

  Cores on computer systems

Assignment : Cores on Computer Systems:  Differentiate between multiprocessor systems and many-core systems in terms of power efficiency, cost benefit analysis, instructions processing efficiency, and packaging form factors.

  Prepare an annual budget in an excel spreadsheet

Prepare working solutions in Excel that will manage the annual budget

  Write a research paper in relation to a software design

Research paper in relation to a Software Design related topic

  Describe the forest, domain, ou, and trust configuration

Describe the forest, domain, OU, and trust configuration for Bluesky. Include a chart or diagram of the current configuration. Currently Bluesky has a single domain and default OU structure.

  Construct a truth table for the boolean expression

Construct a truth table for the Boolean expressions ABC + A'B'C' ABC + AB'C' + A'B'C' A(BC' + B'C)

  Evaluate the cost of materials

Evaluate the cost of materials

  The marie simulator

Depending on how comfortable you are with using the MARIE simulator after reading

  What is the main advantage of using master pages

What is the main advantage of using master pages. Explain the purpose and advantage of using styles.

  Describe the three fundamental models of distributed systems

Explain the two approaches to packet delivery by the network layer in Distributed Systems. Describe the three fundamental models of Distributed Systems

  Distinguish between caching and buffering

Distinguish between caching and buffering The failure model defines the ways in which failure may occur in order to provide an understanding of the effects of failure. Give one type of failure with a brief description of the failure

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