PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000280. Tìm số nguyên tố thứ K — nâng cao (Finding the Kth Prime (Hard))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tìm số nguyên tố thứ K — nâng cao (Finding the Kth Prime (Hard))

Finding the Kth Prime (Hard)

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

List all prime numbers in strictly increasing order, starting from 22: 2,3,5,7,11,13,17,19,…2,3,5,7,11,13,17,19,\ldots.

Given a positive integer KK, find the prime at position KK in this sequence. Positions are numbered starting from 11, so the first prime is 22.

Input

The only line contains a positive integer KK.

Output

Print the KK-th prime as one integer.

Subtasks

  • Subtask 1 (20 points): 1≤K≤10001\le K\le 1000. Time limit: 55 s.
  • Subtask 2 (30 points): 1≤K≤10000001\le K\le 1000000. Time limit: 1010 s.
  • Subtask 3 (50 points): 1≤K≤500000001\le K\le 50000000. Time limit: 2020 s.

Examples

Example 1

Input

1

Output

2

Explanation

The prime sequence starts with 22, which is therefore the first prime.

Example 2

Input

10

Output

29

Explanation

The first ten primes are 2,3,5,7,11,13,17,19,23,292,3,5,7,11,13,17,19,23,29; the tenth is 2929.

Example 3

Input

100

Output

541

Explanation

There are 9999 primes at most 540540, and 541541 itself is prime. Hence 541541 occupies position 100100.

Example 4

Input

10000000

Output

179424673

Explanation

The prime counts satisfy π(179 424 672)=9 999 999\pi(179\,424\,672)=9\,999\,999 and π(179 424 673)=10 000 000\pi(179\,424\,673)=10\,000\,000. Thus 179 424 673179\,424\,673 is the ten-millionth prime.