question archive A prime integer is any integer that is divisible only by itself and l
Subject:Computer SciencePrice:2.86 Bought8
A prime integer is any integer that is divisible only by itself and l. The Sieve of Eratosthenes is a method of ?nding prime numbers. It operates as follows: II Create a list with all elements initialised to l. 0 Starting with list subscript 2, every time the list element is found whose value is 1, loop through the remainder of the list and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For list subscript 2, all elements beyond 2 in the list that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc); for list subscript 3, all elements beyond 3 in the list that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc); and so on. When this process is comPlete, the list elements that are still set to one indicate that the script is a prime number. These subscripts can then be printed. Write a program that uses a list of 1000 elements to determine and print the prime number between 1 and 999. (6%)
import math def prime(): SIZE = 1000 primes = [1]*SIZE end = int(math.sqrt(SIZE)) + 1 # check for 2 to end for i in range(2,end): # if primes[i] is 1 then it is prime if (primes[i]==1): # set all multiples of it to 0 for x in range(i*i,1000,i): primes[x]=0 # print all those numbers which are set as 1 for i in range(2,1000): if(primes[i]==1): print(i,end=" ") prime()# call the prime() function
Please see the attached file for the compete solution