Question

In: Computer Science

PYTHON: Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient...

PYTHON: Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient Greeks. Choose an integer n. This function will compute all prime numbers up to n. First insert all numbers from 1 to n into a set. Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, . . . . Erase all multiples of 3, that is, 6, 9, 12, 15, ... . Go up to √ n. The remaining numbers are all primes.

Please solve in python!

Solutions

Expert Solution

"""
   sieve_of_Eratosthenes prime numbers method : first removing multiples of 2's except 2. From that removing multiples of 3's except 3.
   From that removing multiples of 5's except 5.From that removing multiples of 7's except 7.
"""
def sieve_of_Eratosthenes(n):
   s=[]
   for i in range(0,n):
       s.append(i+1)
   #removing multiples of 2's except 2
   for ele in s:
       if(ele==1):
           s.remove(ele)
       if(ele%2==0):
           if(ele==2):
               continue
           s.remove(ele)
   #removing multiples of 3's except 3.
   for ele in s:
       if(ele%3==0):
           if(ele==3):
               continue
           s.remove(ele)
   #removing multiples of 5's except 5.
   for ele in s:
       if(ele%5==0):
           if(ele==5):
               continue
           s.remove(ele)
   #removing multiples of 7's except 7.
   for ele in s:
       if(ele%7==0):
           if(ele==7):
               continue
           s.remove(ele)
   print("prime nubmers up to ",n,set(s))
#main program
n=int(input("Enter number"))
sieve_of_Eratosthenes(n)


Related Solutions

The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to...
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given limit. Aim Develop code for implementing the sieve of Eratosthenes. Steps for Completion Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise. Consider building the sieve in the class constructor. CODE GIVEN public class SieveOfEratosthenes { public SieveOfEratosthenes(int maxValue) { // Build the sieve here } public boolean isPrime(int value) { // Write...
Here is a (buggy) version of the Eratosthenes sieve: # should return a list of prime...
Here is a (buggy) version of the Eratosthenes sieve: # should return a list of prime numbers from 1..n (including n) def sieve(n): if n < 2: #there are no prime numbers < 2 return [] primes = [2] # 2 is the first prime number for i in range(n): #put all of the numbers into primes initially primes.append(i) for current in range(n): # start at current, and remove all multiples of current up to n for j in range(current,n,current):...
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes. Example Program Session (implement some linefeed ‘\n’ formatting): Enter the limit: 1000 Primes up to 1000    2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53 59   61   67   71   73   79   83   89   97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211...
1. Write a python function that receives two positive numbers and displays the prime numbers between...
1. Write a python function that receives two positive numbers and displays the prime numbers between them.Note: A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself. 2. Using either Whileor Foriteration loops, write a python code that prints the first Nnumbers in Fibonacci Sequence, where N is inputted by the user. Now, revise this code to instead print a) the Nthnumber in Fibonacci Sequence.b) the...
Consider this prime sieve in which an array of numbers of size N is created (i.e....
Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out”...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Read about the Sieve of Sundaram. Implement the algorithm using function composition. Given an integer n,...
Read about the Sieve of Sundaram. Implement the algorithm using function composition. Given an integer n, your function should generate all the odd prime numbers up to 2n+2. sieveSundaram :: Integer -> [Integer] sieveSundaram = ... To give you some help, below is a function to compute the Cartesian product of two lists. This is similar to zip, but it produces all possible pairs instead of matching up the list elements. For example, cartProd [1,2] ['a','b'] == [(1,'a'), (1,'b'), (2,'a'),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT