Works for all cases ..not optimal time..very easy to understand


  • 0
    S

    public class Solution {
    public int countPrimes(int n) {

        int primes=0;
        
        for(int i=2; i<n;i++)
        {
            int count = 1;
            
            for(int j=2;j<=i;j++)
            {
                if(i==j)
                    count++;
                else if(i%j ==0 && i!=j)
                    break;
                else 
                    count++; 
            }
            if(count == i)
                primes++;
        }
        
        return primes;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.