MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / sieveEr

Function sieveEr

other/primelib.py:74–107  ·  view source on GitHub ↗

input: positive integer 'N' > 2 returns a list of prime numbers from 2 up to N. This function implements the algorithm called sieve of erathostenes.

(N)

Source from the content-addressed store, hash-verified

72# ------------------------------------------
73
74def sieveEr(N):
75 """
76 input: positive integer 'N' > 2
77 returns a list of prime numbers from 2 up to N.
78
79 This function implements the algorithm called
80 sieve of erathostenes.
81
82 """
83
84 # precondition
85 assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2"
86
87 # beginList: conatins all natural numbers from 2 upt to N
88 beginList = [x for x in range(2,N+1)]
89
90 ans = [] # this list will be returns.
91
92 # actual sieve of erathostenes
93 for i in range(len(beginList)):
94
95 for j in range(i+1,len(beginList)):
96
97 if (beginList[i] != 0) and \
98 (beginList[j] % beginList[i] == 0):
99 beginList[j] = 0
100
101 # filters actual prime numbers.
102 ans = [x for x in beginList if x != 0]
103
104 # precondition
105 assert isinstance(ans,list), "'ans' must been from type list"
106
107 return ans
108
109
110# --------------------------------

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected