Editor

The Nth Prime

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

The primes in order are 2, 3, 5, 7, 11, 13, ... so 2 is the 1st prime and 13 is the 6th.

Write nth_prime(n) that returns the nth prime number, counting from 1.

Examples

nth_prime(1)      # returns 2
nth_prime(6)      # returns 13
nth_prime(100)    # returns 541

Constraints

  • 1 <= n <= 10,000

Note

You do not know in advance how far to search. A useful fact: for every n >= 6, the nth prime is smaller than n * (ln n + ln ln n), where ln is math.log.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.