vJechsmayr / PythonAlgorithms

All Algorithms implemented in Python 3 a project for hacktoberfest2020 - NO Issues or PRs for hacktoberfest 2021
MIT License
132 stars 367 forks source link

0204 - Count Primes #763

Closed IsuruAriyarathne closed 4 years ago

IsuruAriyarathne commented 4 years ago

Count the number of prime numbers less than a non-negative number, n.

Code

class Solution: def countPrimes(self, n: int) -> int: if (n==0 or n==1 or n==2): return(0) count=0 for num in range(2,n): if num > 1:

check for factors

            for i in range(2,num):
                if (num % i) == 0:
                    #not a prime
                    break 
            else:
                #prime number
                count=count+1
    return (count)

Link To The LeetCode Problem

https://leetcode.com/problems/count-primes/