Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Prime-seiving ) Popular Algorithms | Prime seiving | THE INQUISITIVE





Prime seiving

LEVEL:Beginner

Description

Print all the primes below n

Input Format

single integer n

Output Format

print the prime numbers


Example 1:

Input
3
Output
2
Example 2:

Input
5
Output
2 3
Example 3:

Input
10
Output
2 3 5 7

oops

Login to see Discussion




Approach

Step-i)create array of size n
Step-ii) initialize the array with true
Step-iii) initialize i to 2
Step-iv) if element at index i is true then value at index equal to multiples of i change to false
Step-v) increment i by one value
Step-vi) repeat steps iv and v till i*i is less than the given number
Step-vii) create a empty string answer
Step-viii) append all the index whose value is true
Step-ix) return the string

Time Complexity: (nloglogn)
Space Complexity:


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution