Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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)

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92525202
  • Price:- $30

Priced at Now at $30, Verified Solution

Have any Question?


Related Questions in Computer Engineering

A set of coins makes change fornbspnnbspif the sum of the

A set of coins makes change for n if the sum of the values of the coins is n. For example, if you have 1-cent, 2-cent and 4-cent coins, the following sets make change for 7: 7 1-cent coins 5 1-cent, 1 2-cent coins 3 1-ce ...

Discuss the importance of using an access control model in

Discuss the importance of using an access control model in determining how employees in an organization should gain access to resources.

Question suppose that we want to create a divide and

Question : Suppose that we want to create a divide and conquer matrix multiplication algorithm for square matrices. Assuming that n is a power of three, the algorithm divides each matrix of A, D, and C into nine equi-siz ...

Relational algebrawrite a relational algebra expression

[Relational Algebra] Write a relational algebra expression that will output first name and last name of computer science major male students who have borrowed books from "University of Iowa Main Library". Relational Sche ...

Problem belowwrite a program that uses a function that

Problem below: Write a program that uses a function that returns a number between 1 and 6. Use this function to simulate the roll of a die. Allow the user to specify the number of trials and then tabulate that number of ...

Systems analysis project 12 please answer the 2 questions1

Systems analysis project 12: please answer the 2 questions 1. Develop a process for managing change requests and design a form to handle a generic change request. The process should include a contingency plan for changes ...

Question define four different conflicts you have

Question: Define four different conflicts you have encountered. These conflicts can be work related or personal conflicts. Need in APA Format, 300 words, No Plagiarism. The response must be typed, single spaced, must be ...

Question suppose that an organization did not use processes

Question : Suppose that an organization did not use processes that were designed and built to be agile, particularly when it comes to IS / IT technologies. Predict the impact of environmental changes on an organization f ...

Question write a minimum of 100 words for each question

Question: Write a minimum of 100 words for each question. Provide Citation and reference for each question. Provide two responses for each question. 1. Let us suppose we are designing a computer for use for a graphic art ...

Taskstudents are required to do the following tasks for

Task Students are required to do the following tasks for write report by answering all the questions at the end of case study: Task a: Answering all the questions at the end of case study. Task b: Student is required to ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As