LeetCode[6] - Count Primes

方法很牛逼也很数学。没做的时候可能想不到。做了之后就觉得,哎,我去,有道理啊。
简而言之:简历一个boolean长条,存isPrime[]。 然后从i=2, 全部变true.
然后利用这个因子的性质,非prime满足条件: self*self, self * self + self … etc.
所以就check每一个j, j+i, j+i+i, 然后把所有non-prime全部mark成false.
最后,数一遍还剩下的true个数就好了

/*
Description:

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

Tags: Hash Table, Math
Similar Problems: (E) Ugly Number, (M) Ugly Number II, (M) Perfect Squares

*/

/*
Attempt2: https://leetcode.com/problems/count-primes/ explains it well
1. Ignore 1 and n. Don't count 1 and the number itself in.
2. Assume all numbers are prime in a boolean[]. Check off those are certainly not prime, the remaining will be prime.
3. For any n, only need to check up to i * i < n; more than that, for example 2 x 6 is same as checking 6x2, but 6x2 is not necessary to check.
4. How to mark things off:
    The first non-prime is always i^2: self * self.
    Then more non-primes:self * self, self * (self + 1), self * (self + 2) ... etc.
    So, mark all of these index of in the boolean[]
    
*/
public class Solution {
    public int countPrimes(int n) {
        if (n <= 1) {
            return 0;
        }
        boolean[] primes = new boolean[n];
        for (int i = 2; i < primes.length; i++) {
            primes[i] = true;
        }

        for (int i = 2; i * i< n; i++) {
            if (!primes[i]) {
                continue;
            }
            for (int j = i * i; j < n; j += i) {
                primes[j] = false;
            }
        }
        int count = 0;
        for (int i = 2; i < primes.length; i++) {
            count += primes[i] ? 1 : 0;
        }
        return count;
    }
}


/*
1st attempt, ofcourse timeout.
Thoughts:

Prime: number > 1 and has not integer divider rather than 1

Trivial way: for all (1~n-1), do while loop to check prime on each number m. O(mn)
*/

public class Solution {
    public int countPrimes(int n) {
        if (n <= 1) {
            return 0;
        } 
        int count = 0;
        for (int i = 1; i < n; i++) {
            int countDividers = 0;
            for (double j = 1; j <= i; j++) {
                countDividers += (i / j) % 1 == (i / j) ? 1 : 0;
            }
            if (countDividers <= 2) {
                count++;
            }
        }
        return count;
    }
}
    原文作者:张土汪
    原文地址: https://www.jianshu.com/p/d17542cafcc3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞