Sieve of Eratosthenes | Practice | GeeksforGeeks

Given a number N, calculate the prime numbers up to N using Sieve of Eratosthenes.  
Example 1:
Input:
N = 10

Output:
2 3 5 7

Explanation:
Prime numbers less than equal to N 
are 2 3 5 and 7.
Example 2:
Input:
N = 35

Output:
2 3 5 7 11 13 17 19 23 29 31

Explanation:
Prime numbers less than equal to 35 are
2 3 5 7 11 13 17 19 23 29 and 31.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function sieveOfEratosthenes() which takes an integer N as an input parameter and return the list of prime numbers less than equal to N.
Expected Time Complexity: O(NloglogN)
Expected Auxiliary Space: O(N)
Constraints:
1<= N <= 104
0
pranavkhandare45452 weeks ago
JAVA SOLUTION
 
static ArrayList<Integer> sieveOfEratosthenes(int N){       ArrayList<Integer> al=new ArrayList<>();       boolean isprime[]=new boolean[N+1];       Arrays.fill(isprime,true);              for(int i=2;i*i<=N;i++){           if(isprime[i]){               for(int j=2*i;j<=N;j=j+i){                   isprime[j]=false;               }           }       }       for (int i=2;i<=N;i++){           if(isprime[i]==true){               al.add(i);           }       }       return al;   }
 
 
 
 
 
 
 
 
 
 
 
 
 
0
mail2rajab011 month ago
// Simple Java Solution
Total Time Taken: 0.6/1.5
 
class Solution{   static ArrayList<Integer> sieveOfEratosthenes(int N){       // code here       ArrayList<Integer> al=new ArrayList<> ();       boolean arr[]=new boolean[N+1];       Arrays.fill(arr,true);       arr[0]=false;       arr[1]=false;              for(int i=2;i*i<=N;i++){           for(int j=2*i;j<=N;j +=i){               arr[j]=false;           }       }              for(int i=0;i<arr.length;i++){           if(arr[i]==true){               al.add(i);           }       }       return al;   }}
0
sharmanitish1832 months ago
static ArrayList<Integer> sieveOfEratosthenes(int N){       ArrayList<Integer> a=new ArrayList<>();       boolean prime[]=isPrime(N);       for(int i=2;i<=N;i++){           if(prime[i])           a.add(i);       }              return a;         }   public static boolean[] isPrime(int n){       boolean a[]=new boolean[n+1];       Arrays.fill(a,true);       a[0]=false;       a[1]=false;       for(int i=2;i<=Math.sqrt(n);i++){           for(int j=2*i;j<=n;j+=i)           a[j]=false;       }       return a;   }
0
deepakkoshta8063 months ago
vector<int> sieveOfEratosthenes(int N)
   {
       int i,j;
       vector<int> v;
       for(i=2;i<=N;i++)
       {
           for(j=2;j<=i/2;j++)
               if(i%j==0)
                   break;
           if(j==(i/2)+1)
               v.push_back(i);
       }
       return v;
   }
0
akashjoy5983 months ago
Simple Code:- 
class Solution{   static ArrayList<Integer> sieveOfEratosthenes(int N){       // code here       ArrayList<Integer>prime = new ArrayList<>();       for(int i = 2;i<=N;i++){           boolean isprime = true;           int div = 2;           while(div*div<=i){               if(i%div==0){                   isprime = false;                   break;               }               div++;           }           if(isprime==true){               prime.add(i);           }       }       // System.out.println(N);       return prime;   }}
0
puspdev4 months ago
class Solution:   def sieveOfEratosthenes(self, N):    #code here     res = [0]*(N+1)              i = 2       ans = []       while (i*i <=N):           if not res[i]:               for j in range(i*i, N+1, i):                   res[j]=1           i+=1       for j in range(2, N+1):           if not res[j]:               ans.append(j)       return ans
0
puspdev
This comment was deleted.
+1
princeagrawal6174 months ago
vector<int>primes;   bool arr[N+1]={false};   arr[0]=arr[1]=true;
   for(int i=2;i*i<=N;i++){       if(arr[i]==false){           for(int j=i*i;j<=N;j+=i)           arr[j]=true;       }   }
   for(int i=2;i<=N;i++){       if(!arr[i])       primes.push_back(i);   }   return primes;   }
0
xixo5 months ago
//User function Template for Javaclass Solution{   static ArrayList<Integer> sieveOfEratosthenes(int N){       boolean[] criba = new boolean[N+1];       ArrayList<Integer> primes = new ArrayList<Integer>();       primes.add(2);       for(int i=3; i<=N; i+=2){           if(!criba[i]){               primes.add(i);               for(int j=i*i;j<=N;j+=2*i){                   criba[j]=true;               }           }       }       return primes;   }}
+1
krishanu2020ca0355 months ago
 vector<int> sieveOfEratosthenes(int n)
    {
        // Write Your Code here
        if(n <= 1)
		return {};
		
        bool isPrime[n + 1];
        fill(isPrime, isPrime + n + 1, true);
        
        for(int i=2; i*i <= n; i++)
    	{
    		if(isPrime[i])
    		{
    			for(int j = 2*i; j <= n; j = j+i)
    			{
    				isPrime[j] = false;
    			}
    		}
    	}
        
        vector<int> Primes;
        for(int i = 2; i <= n; i++)
        {
            if(isPrime[i])
            Primes.push_back(i);
        }
        
        return Primes;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.