easy java solution


  • 0
    U
    class Solution {
        public int countPrimes(int n) {
      int res=0;
      for(int i=0;i<=n;i++){
        if(prime(i)==true){
          //System.out.println("prime numbe ris : "+i);
          res++;
        }
      }
      return res;
    }
    
            
        public boolean prime(int n) {
      if(n==1||n==0) return false;
      if(n==2||n==3 || n==5 || n==7 || n==11) return true;
    
     if(n%2==0 || n%3==0){
     return false;
    }
     for(int i=5;i<n;i++){
       if(n%i==0){
         return false;
       }
     }
     return true;
    
     }
    }
    

Log in to reply
 

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