Skip to main content

Prime Numbers

Medium

Write a function find_primes(n) that returns all prime numbers less than or equal to n.

Examples

Python
find_primes(1) # => [] find_primes(5) # => [2, 3, 5] find_primes(20) # => [2, 3, 5, 7, 11, 13, 17, 19]

A prime number is defined as any number that cannot be evenly divided by any other number.

Once we've found a prime number, what else do we know? For example, we know 2 is the only even prime number because all others are multiples of 2.

Could we pre-compute the multiples of prime numbers we've found so far?

Does your solution run in O(n^2) time? Can you make it faster?

Do you perform any extra computations that can be optimized?