Given the value of n, print the n'th prime number. Input : A single integer n. Output :…
- User
Given the value of n, print the n'th prime number. Input : A single integer n. Output : A single number which is the n'th prime number. Constraints : 1 ≤ n ≤ 1000
SAMPLE INPUT 2
SAMPLE OUTPUT 3
Explanation
The first few prime numbers are: 2,3,5,7. So, the answer is 3.
Assistant
We need to implement a solution to output the n-th prime given n up to 1000. Since n max is 1000, the 1000th prime is known: 7919 (I recall from memory that the 1000th prime is 7919). Actually let's verify: primes: 2,3,5,7,11,... 1000th prime is 7919? I know that 1000th prime is 7919. Quick check: yes, commonly known. So we could precompute up to 7919. But the problem expects an algorithmic approach. However, since constraints are small, we can just generate primes until we have n primes.
We need to parse input, n integer. Then compute prime numbers sequentially starting from 2, checking primality. For each number, if prime, count++. When count==n, print the number.
Simplest solution: Use simple trial division up to sqrt(num). Since numbers up to ~8000, it's fine.
We can also precompute a list of primes using sieve of Eratosthenes up to some bound. Since we don't know exact bound but we know 1000th prime < 10000 perhaps (actually 7919 < 10000). So we can sieve up to 10000. That's efficient.