Editor

Primes Up to N

#72 · Math · Intermediate · 65 XP · about 12 min

A prime is a whole number greater than 1 whose only divisors are 1 and itself: 2, 3, 5, 7, 11 and so on.

Write primes_up_to(n) that returns a list of every prime from 2 up to and including n, smallest first. If there are none, return an empty list.

Examples

primes_up_to(10)    # returns [2, 3, 5, 7]
primes_up_to(2)     # returns [2]
primes_up_to(1)     # returns []

Constraints

  • 0 <= n <= 200,000
  • A good solution handles n = 200,000 in well under a second.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.