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

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 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