Seive of Erathostenis

Learnings

To generate Primes in a range from 1 to N Start from 2 and mark all Divisors of 2 as false So on for 3 and continue

Intuition

    vector<int> seive(int n){
        vector<int> prime(n+1, 1);
        prime[0] = 0;
        prime[1] = 0;

        for(int p=2; p*p<=n; p++){
            if(prime[p] == true){
                for(int i=p*p; i<=n; i+=p){
                    prime[i] = false;
                }
            }
        }
        
        // Ones for all Those are prime Till N

        return prime;
    }

https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/

Last updated