MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / get_prime_numbers

Function get_prime_numbers

maths/primelib.py:141–173  ·  view source on GitHub ↗

input: positive integer 'N' > 2 returns a list of prime numbers from 2 up to N (inclusive) This function is more efficient as function 'sieveEr(...)' >>> get_prime_numbers(8) [2, 3, 5, 7] >>> get_prime_numbers(-1) Traceback (most recent call last): ... Asser

(n)

Source from the content-addressed store, hash-verified

139
140
141def get_prime_numbers(n):
142 """
143 input: positive integer 'N' > 2
144 returns a list of prime numbers from 2 up to N (inclusive)
145 This function is more efficient as function 'sieveEr(...)'
146
147 >>> get_prime_numbers(8)
148 [2, 3, 5, 7]
149 >>> get_prime_numbers(-1)
150 Traceback (most recent call last):
151 ...
152 AssertionError: 'N' must been an int and > 2
153 >>> get_prime_numbers("test")
154 Traceback (most recent call last):
155 ...
156 AssertionError: 'N' must been an int and > 2
157 """
158
159 # precondition
160 assert isinstance(n, int) and (n > 2), "'N' must been an int and > 2"
161
162 ans = []
163
164 # iterates over all numbers between 2 up to N+1
165 # if a number is prime then appends to list 'ans'
166 for number in range(2, n + 1):
167 if is_prime(number):
168 ans.append(number)
169
170 # precondition
171 assert isinstance(ans, list), "'ans' must been from type list"
172
173 return ans
174
175
176# -----------------------------------------

Callers 1

goldbachFunction · 0.85

Calls 2

is_primeFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected