LeetCode 204. Count prime (Python)

Topic Link

Subject description:

Prime number less than the number of all non-negative integer n statistics.

Example:

Input: 10
Output: 4
Explanation: prime number less than 10, a total of four, are 2, 3, 5, 7.

Solution 1:
Eladuosai sieve method:
First each of the number of 2-N into a table, and then draw a circle in the above 2, and then to draw other multiples of 2; neither the first nor the circle the number 3 is crossed out, it circle, and then to draw other multiples of 3; now neither a first number has not been crossed out circle is 5, it circle, and to draw other multiples of 5 ...... and so on, until all of each number is less than or equal to N are drawn circle or crossed out so far. In this case, the circle drawn in the table, and those numbers are not exactly crossed out is smaller than the prime number N.

In implementation, one may be provided an array of length n, all values set to 1.
Then the index number 2 is traversing the array, the first prime number is 2, count by 1, the index of all multiples of 2 is set to a multiple of the number of 0. 2 until n becomes greater than
continue to iterate and find the next number is not 0, a digital set count by 1, the index for the multiple number is 0, up until a factor greater than n

class Solution(object):
    def countPrimes(self, n):
        """
        :type n: int
        :rtype: int
        """
        a=[1]*n
        count=0
        for i in range(2,n):
            if a[i]==1:
                count+=1 
                j=1
                while i*j<n:
                    a[i*j]=0
                    j+=1
        return count

Here Insert Picture Description
Comments district less time with the wording:

        arr=[1]*n
        arr[0],arr[1]=0,0
        for i in range(2,int(n**0.5)+1):
            if arr[i]:
                arr[i*i:n:i]=[0]*((n-1)//i-i+1)
        return sum(arr)

Solution 2:
intuitive written, is a prime number only divisible by itself and 1:

This approach this question timeout

        num=0
        for i in range(2,n):
            flag=1
            for j in range(2,i):
                if i%j==0:
                    flag=0
                    break
            if flag==1:
                num+=1
        return num

Solution 3:
prime number there is a feature that it is always equal to 6x-1 or 6x + 1, where x is a natural number greater than or equal to 1.
First 6x certainly not a prime number because it is divisible by 6; secondly 6x + 2 is certainly not a prime number because it is divisible by 2; and so on, 6x + 3 certainly divisible by 3; 6x + 4 definitely divisible by 2 . Then, only 6x + 1 and 6x + 5 (i.e., equivalent to 6x-1) may be a prime number. So the loop can be set in steps of 6, and then determines a time to the number 6 on both sides.

This method is also this question timeout

        count=0
        for num in range(n):
            if num==1:
                continue
            if num == 2 or num == 3:   # 两个较小的数进行处理
                count+=1
                continue
            if num % 6 != 1 and num % 6 != 5:  # 不在6的倍数的两侧的一定不是质数
                continue
            tmp = int(math.sqrt(num))
            flag=1
            for i in range(5, tmp+1):  # 在6的倍数两侧的也可能不是质数
                if num % i == 0:
                    flag=0
            if flag==1:count+=1
        return count

Guess you like

Origin blog.csdn.net/weixin_44740082/article/details/91501864
Recommended