ASSIST - Assistance Required

no tags 

After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty dishes. The contestants would line up in a queue, one behind the other. Each contestant got a number starting with 2 for the first one, 3 for the second one, 4 for the third one, and so on, consecutively.

The first contestant in the queue was asked for his number (which was 2). He was freed from the washing up and could party on, but every second contestant behind him had to go to the kitchen (those with numbers 4, 6, 8, etc). Then the next contestant in the remaining queue had to tell his number. He answered 3 and was freed from assisting, but every third contestant behind him was to help (those with numbers 9, 15, 21, etc). The next in the remaining queue had number 5 and was free, but every fifth contestant behind him was selected (those with numbers 19, 35, 49, etc). The next had number 7 and was free, but every seventh behind him had to assist, and so on.

Let us call the number of a contestant who does not need to assist with washing up a lucky number. Continuing the selection scheme, the lucky numbers are the ordered sequence 2, 3, 5, 7, 11, 13, 17, etc. Find out the lucky numbers to be prepared for the next contest party.

Input Specification

The input contains several test cases. Each test case consists of an integer n. You may assume that 1 <= n <= 3000. A zero follows the input for the last test case.

Output Specification

For each test case specified by n output on a single line the n-th lucky number.

Sample Input

1
2
10
20
0

Sample Output

2
3
29
83

hide comments
spa1ish: 2018-05-02 17:04:33

AC using vector..iterator...erase()

saurav52: 2017-08-30 20:39:07

Use list for c++ and its done :)

madhavgaba: 2016-12-28 11:05:33

its like brute-force cum sieve -_-

code_inception: 2016-11-12 11:50:43

@praney_rai read the question creafully ..... after 5 every fifth person goes for assistance i.e., 19 35 49 not 10 15 20 25 .....

praney_rai: 2016-09-30 09:23:40

20 th prime number is 71 but here it is shown 83. is there any misunderstanding

praval_singhal: 2016-06-18 18:27:35

I tried a lot but couldn't perform better than 0.02s. Any Suggestions by those who got 0.00s. Thanks in Advance.

sravanthreddy: 2016-03-24 19:25:32

use sieve==>bruteforce... ;) AC yay!

gullu_mishra: 2016-03-12 08:18:34

dont think too much ...use brute force ... ;)

dwij28: 2016-01-02 11:28:41

AC using a sieve. I tried for 5 minutes but could not think of an O(1) solution or a formula. Is O(1) possible ?

:.Mohib.:: 2015-06-10 09:31:52

Easy ;)


Added by:Wanderley Guimarăes
Date:2007-09-21
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO
Resource:University of Ulm Local Contest 2003