Find prime numbers

You are given a positive integer ‘N’. Your task is to print all prime numbers less than or equal to N.

Note: A prime number is a natural number that is divisible only by 1 and itself. Example - 2, 3, 17, etc.

You can assume that the value of N will always be greater than 1. So, the answer will always exist.

Input Format:
The input contains a single positive integer ‘N’.


Output Format :
Print single space-separated prime numbers less than or equal to ‘N’ in increasing order.

Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints:
2 <= N <= 10^7

Where ‘N’ is the given positive integer.

Time Limit: 1sec
CodingNinjas
author
2y

Approach (Using Sieve of Eratosthenes) :

1) First, we will make a boolean array/list isPrime of size N + 1. This will mark if a number is prime or not. Initially, all
values will be true.

2) Then, we ini...read more

CodingNinjas
author
2y
Brute force approach

We use Brute Force to solve this problem.

  1. We iterate from 2 to N.
  2. For each number num, we check if it is prime or not. We use the following approach to check if a given number is pr...read more
CodingNinjas
author
2y
Optimized primality test

We will optimize the test to check if a given number is prime. Instead of looping to N-1, we check divisibility till (N)^½. This is because if the number is not prime, there mu...read more

CodingNinjas
author
2y
Sieve of Eratosthenes

We will use the Sieve of Eratosthenes to solve this problem.

 

  1. First, we will make a boolean array/list isPrime of size N + 1. This will mark if a number is prime or not. Initially, all values will be true.
  2. Then, we initialize a variable num equal to 2 which represents the current processing prime number.
  3. We will loop as num from 2 to N^½:
    1. If num is not prime, we continue.
    2. Else, we will mark all multiples of num in isPrime as false.
  4. In the end, we will iterate through isPrime and store all primes in the result vector/list.
  5. Finally, we return the result vector/list.
Space Complexity: O(1)Explanation:

O(N), where N is the given positive integer.

 

We create a boolean array/list isPrime to store all numbers from 2 to N

So, the space complexity is O(N).

Time Complexity: O(1)Explanation:

O(N * log(log N)) where N is the given positive integer. 

 

The algorithm will perform N / p operations for every prime p ≤ N in the inner loop. So, the number of times the loop runs is equal to ΣN/p = N* Σ1/p where p ≤ N.

The value of  Σ1/p is log(log N). So, the overall time complexity is O(N *log(log N)).

 

For a detailed explanation, visit here.

Add answer anonymously...
Bosch Global Software Technologies Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter