시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB2082311268951354.877%

문제

에라토스테네스의 체는 N보다 작거나 같은 모든 소수를 찾는 유명한 알고리즘이다.

이 알고리즘은 다음과 같다.

  1. 2부터 N까지 모든 정수를 적는다.
  2. 아직 지우지 않은 수 중 가장 작은 수를 찾는다. 이것을 P라고 하고, 이 수는 소수이다.
  3. P를 지우고, 아직 지우지 않은 P의 배수를 크기 순서대로 지운다.
  4. 아직 모든 수를 지우지 않았다면, 다시 2번 단계로 간다.

N, K가 주어졌을 때, K번째 지우는 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N과 K가 주어진다. (1 ≤ K < N, max(1, K) < N ≤ 1000)

출력

첫째 줄에 K번째 지워진 수를 출력한다.

예제 입력 1

7 3

예제 출력 1

6

예제 입력 2

15 12

예제 출력 2

7

예제 입력 3

10 7

예제 출력 3

9

2, 4, 6, 8, 10, 3, 9, 5, 7 순서대로 지워진다. 7번째 지워진 수는 9이다.

W3sicHJvYmxlbV9pZCI6IjI5NjAiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWM1ZDBcdWI3N2NcdWQxYTBcdWMyYTRcdWQxNGNcdWIxMjRcdWMyYTRcdWM3NTggXHVjY2I0IiwiZGVzY3JpcHRpb24iOiI8cD5cdWM1ZDBcdWI3N2NcdWQxYTBcdWMyYTRcdWQxNGNcdWIxMjRcdWMyYTRcdWM3NTggXHVjY2I0XHViMjk0IE5cdWJjZjRcdWIyZTQgXHVjNzkxXHVhYzcwXHViMDk4IFx1YWMxOVx1Yzc0MCBcdWJhYThcdWI0ZTAgXHVjMThjXHVjMjE4XHViOTdjIFx1Y2MzZVx1YjI5NCBcdWM3MjBcdWJhODVcdWQ1NWMgXHVjNTRjXHVhY2UwXHViOWFjXHVjOTk4XHVjNzc0XHViMmU0LjxcL3A+XHJcblxyXG48cD5cdWM3NzQgXHVjNTRjXHVhY2UwXHViOWFjXHVjOTk4XHVjNzQwIFx1YjJlNF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