28 lines
804 B
Python
28 lines
804 B
Python
def count_up_to(n):
|
|
"""Implement a function that takes an non-negative integer and returns an array of the first n
|
|
integers that are prime numbers and less than n.
|
|
for example:
|
|
count_up_to(5) => [2,3]
|
|
count_up_to(11) => [2,3,5,7]
|
|
count_up_to(0) => []
|
|
count_up_to(20) => [2,3,5,7,11,13,17,19]
|
|
count_up_to(1) => []
|
|
count_up_to(18) => [2,3,5,7,11,13,17]
|
|
"""
|
|
def is_prime(num):
|
|
if num < 2:
|
|
return False
|
|
if num == 2:
|
|
return True
|
|
if num % 2 == 0:
|
|
return False
|
|
for i in range(3, int(num ** 0.5) + 1, 2):
|
|
if num % i == 0:
|
|
return False
|
|
return True
|
|
|
|
primes = []
|
|
for i in range(2, n):
|
|
if is_prime(i):
|
|
primes.append(i)
|
|
return primes |