Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1.  
  2. 6.12 LAB: Sieve of Eratosthenes
  3.  
  4. In this lab, you are going to implement "Sieve of Eratosthenes" (SoE) into a Python function.
  5.  
  6. The input of SoE is an integer n, which denotes the maximum integer you are going to test whether or not every integer less than or equal to this number is a prime number.
  7.  
  8. The algorithm of SoE is described as follows (Photo credit: Wikipedia https://en.wikipedia.org/wiki/SieveofEratosthenes#/media/File:SieveofEratosthenes_animation.gif):
  9.  
  10. First of all, a prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself.
  11.  
  12. To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
  13.  
  14. Below is what you need to implement in function SieveOfEratosthenes()
  15.  
  16. Create a list, or preferably a Python dictionary, corresponds to consecutive integers from 2 through n: (2, 3, 4, …, n). You might want to initialize what you've just created with a long array of Boolean True values to denote that all integers are initially assumed to be prime numbers (and sift away composite numbers in the following steps). For dictionaries, key the dictionary with (2, 3,4,…,n) and intialize all values to True.
  17.  
  18. prime_number_table = [True for _ in range(2, n+1) ] # This is a list comprehension example.
  19. prime_number_table = {k: True for k in range(2, n+1) } # This is a dictionary comprehension example.
  20.  
  21. Initially, let p equal 2, the smallest prime number.
  22. Enumerate the multiples of p by counting to n from 2p in increments of p, and mark them in the list (these will be 2p, 3p, 4p, …; the p itself should not be marked). For example, whenever you find the first prime number "2," it automatically follows that all multiples of 2 are composite numbers (i.e. the exact opposite of prime number). So mark 4, 6, 8, 10, 12 as False in your list or dictionary. Repeat the process whenever you find another prime number.
  23.  
  24. # Following code snippet only works with one particular definition of prim_number_table provided above.
  25. prime_number_table[2*2] = False
  26. prime_number_table[2*3] = False
  27. prime_number_table[2*4] = False
  28. prime_number_table[2*5] = False
  29. ...
  30.  
  31. The second round of sifting away composite number should do:
  32.  
  33. # Following code snippet only works with one particular definition of prim_number_table provided above.
  34. prime_number_table[3*2] = False
  35. prime_number_table[3*3] = False
  36. prime_number_table[3*4] = False
  37. prime_number_table[3*5] = False
  38. ...
  39.  
  40. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.
  41. When the algorithm terminates, the numbers remaining not marked in the list are all the primes below n.
  42.  
  43. Example of input-output pair is shown below:
  44.  
  45. When input is
  46.  
  47. 10
  48.  
  49. Your program is expected to output:
  50.  
  51. [2, 3, 5, 7]
  52.  
  53. When input is
  54.  
  55. 100
  56.  
  57. Your program is expected to output:
  58.  
  59. [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]\n
  60.  
  61. Since the list of prime number starts from 2. Whenever you get an input less than 2, return "invalid input"
  62.  
  63. When input is
  64.  
  65. 1
  66.  
  67. Your program is expected to output:
  68.  
  69. invalid input
  70.  
  71. In addition, your program should reject any invalid user input by print out "invalid input" whenever it receives a floating point number or any other input which doesn't make sense to "Sieve of Eratosthenes"
  72.  
  73. When input is
  74.  
  75. 3.7
  76.  
  77. Your program is expected to output:
  78.  
  79. invalid input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement