Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Primes upto N Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Primes upto N | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a number, find all the prime numbers from 1 to that number.

Naive Approach

We can iterate over all the numbers from 1 to N, and check if the current number is prime, store it in an appropriate container.

Analysis

  • Time Complexity: O(n * root(n)) // root(n) is required to check if a number is prime.
  • Auxiliary Space Complexity: O(1)

Implementation

C++
bool isPrime(int n) {
   for(int i = 2; i * i <= n; i++) {
       if(n % i == 0) {
           return false;
       }
   }
   return true;
}
vector<int> primesUptoN(int n) {
   vector<int> answer;
   for(int i = 2; i <= n; i++) {
       if(isPrime(i) == true) {
           answer.push_back(i);
       }
   }
   return answer;
}
Java
class Solution {
   boolean isPrime(int n) {
       for(int i = 2; i * i <= n; i++) {
           if(n % i == 0) {
               return false;
           }
       }
       return true;
   }
   List<Integer> primesUptoN(int n) {
       ArrayList<Integer> answer = new ArrayList<Integer>();
       for(int i = 2; i <= n; i++) {
           if(isPrime(i) == true) {
               answer.add(i);
           }
       }
       return answer;
   }
}

Optimal Approach

We can precompute all the primes from 1 to N in N * log(logN) time complexity using the Sieve of Eratosthenes.

In this algorithm, we initially consider all the numbers from 1 to N to be primes, then we iterate over all the numbers from 2 to sqrt(N), and if we find a prime number, then we mark all its multiples as non-prime.

At the end of the algorithm, only the prime numbers remain unmarked and so we can decide if a number is prime in constant time.

Analysis

  • Time Complexity: O(N * log(logN))
  • Space Complexity: O(n)

Implementation

C++
vector<int> primesUptoN(int n) {
   bool isPrime[n + 1];
   for(int i = 2; i <= n; i++) {
       isPrime[i] = true;
   }
   isPrime[0] = isPrime[1] = false;
   for(int i = 2; i * i <= n; i++) {
       for(int j = i * i; j <= n; j += i) {
           if(isPrime[i] == true) {
               isPrime[j] = false;
           }
       }
   }
   vector<int> answer;
   for(int i = 2; i <= n; i++) {
       if(isPrime[i] == true) {
           answer.push_back(i);
       }
   }
   return answer;
}
Java
class Solution {
   List<Integer> primesUptoN(int n) {
       boolean isPrime[] = new boolean[n + 1];
       for(int i = 2; i <= n; i++) {
           isPrime[i] = true;
       }
       isPrime[0] = isPrime[1] = false;
       for(int i = 2; i * i <= n; i++) {
           for(int j = i * i; j <= n; j += i) {
               if(isPrime[i] == true) {
                   isPrime[j] = false;
               }
           }
       }
       ArrayList<Integer> answer = new ArrayList<Integer>();
       for(int i = 2; i <= n; i++) {
           if(isPrime[i] == true) {
               answer.add(i);
           }
       }
       return answer;
   }
}
Related Content
Clone List with Random Pointer
Cumulative Sum
Distinct Numbers in Window
Even Number of Digits
Excel Column Number
Exponentiation With Modulus
Four Sum
Greatest Common Divisor
Identical Twins
Identical Twins
Implement Counting Sort
Largest Contiguous Sum
Longest Consecutive Sequence
Longest Subarray with Zero Sum
Longest Substring with K Unique Characters
Longest Substring Without Repeat
LRU Cache
Matrix Paths
Matrix Rotation
Max Consecutive Ones
Next Greater Permutation
Non-Repeating Element
Pascal's Triangle
Positive Cumulative Sum
Row Column Zero
Subarrays With Given XOR
Three Sum
Trailing Zeroes
Two Sum
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet