Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-048

N-th Prime

Problem

Return the n-th prime number with 1-indexed counting, so nth_prime(1) is 2 and nth_prime(10) is 29.

Test candidate integers in increasing order and count primes until reaching n, checking divisibility only up to the square root. Return -1 for n < 1.

Hint

A number is prime when no divisor up to its square root divides it evenly.

Test cases

case 1
in: [1]
expected: 2
case 2
in: [2]
expected: 3
case 3
in: [6]
expected: 13
case 4
in: [10]
expected: 29
case 5
in: [100]
expected: 541
Starter codePython
def nth_prime(n):
    # Your code here
    pass

5 test cases · run your code against them in the editor