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

#SP0000264. Số nguyên tố tiếp theo (Next Prime)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Số nguyên tố tiếp theo (Next Prime)

Next Prime

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Given a positive integer nn, find the smallest prime strictly greater than nn. Even if nn is already prime, the answer must exceed it.

Input

One line containing the integer nn.

Output

Print one integer: the smallest prime greater than nn.

Subtasks

  • Subtask 1 (20 points): 1≤n≤100001\le n\le 10000.
  • Subtask 2 (30 points): 1≤n≤1081\le n\le 10^8.
  • Subtask 3 (50 points): 1≤n≤10121\le n\le 10^{12}.

Examples

Example 1

Input

1

Output

2

Explanation

The smallest prime strictly greater than 11 is 22.

Example 2

Input

2

Output

3

Explanation

Although 22 is prime, the required number must be strictly greater, hence 33.

Example 3

Input

42

Output

43

Explanation

43>4243>42 and 4343 has no prime divisor among 2,3,52,3,5, so the answer is 4343.

Example 4

Input

1337

Output

1361

Explanation

Every even number from 13381338 through 13601360 is divisible by 22. The odd numbers are also composite:

  • 1339=13⋅1031339=13\cdot103, 1341=3⋅4471341=3\cdot447, 1343=17⋅791343=17\cdot79.
  • 1345=5⋅2691345=5\cdot269, 1347=3⋅4491347=3\cdot449, 1349=19⋅711349=19\cdot71.
  • 1351=7⋅1931351=7\cdot193, 1353=3⋅4511353=3\cdot451, 1355=5⋅2711355=5\cdot271.
  • 1357=23⋅591357=23\cdot59, 1359=3⋅4531359=3\cdot453.

Thus no integer between 13381338 and 13601360 is prime. Since 1361<37\sqrt{1361}<37, checking the primes 2,3,5,7,11,13,17,19,23,29,312,3,5,7,11,13,17,19,23,29,31 is sufficient; none divides 13611361. Therefore the first prime greater than 13371337 is 13611361.