Advertisement
Iam_Sandeep

204. Count Primes

Aug 1st, 2022
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1.  
  2. class Solution:
  3.     def countPrimes(self, n: int) -> int:
  4.         if n<=2:return 0
  5.         a=[True]*(n)
  6.         a[0]=a[1]=False
  7.         a[2]=True
  8.         i=2
  9.         while i**2<n:
  10.             if a[i]:
  11.                 for j in range(i**2,n,i):
  12.                     a[j]=False
  13.             i+=1
  14.        
  15.         return a.count(True)
  16.  
  17.  
  18. class Solution:
  19.     def countPrimes(self, n: int) -> int:
  20.         if n<=2:return 0
  21.         a=[True]*(n)
  22.         a[0]=a[1]=False
  23.         a[2]=True
  24.         for i in range(2,int(n**0.5)+1):
  25.             if a[i]:
  26.                 j=i**2
  27.                 while j<n:
  28.                     if j%i==0:a[j]=False
  29.                     j=j+i
  30.        
  31.         return a.count(True)
  32.      
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement