836. Print all primes till N

You are given an integer n.

Print all the prime numbers till n (including n).

A prime number is a number that has only two divisor's 1 and the number itself.

Example 1:

Input : n = 7

Output : [2, 3, 5, 7]

Explanation : The number 2 has only two divisors 1 and 2.

The number 3 has only two divisors 1 and 3.

The number 5 has only two divisors 1 and 5.

The number 7 has only two divisors 1 and 7.

Example 2:

Input : n = 2

Output : [2]

Explanation : There is only one number 2 that is a prime till 2.

Now Your Turn!

Pick the correct output for the given input

Input : n = 10

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 <= n <= 5*105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution{
public:
vector<int> primeTillN(int n){
//your code goes here
}
};
Test Case

Input:

N