Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

计数质数 #8

Open
Aras-ax opened this issue Mar 5, 2019 · 0 comments
Open

计数质数 #8

Aras-ax opened this issue Mar 5, 2019 · 0 comments

Comments

@Aras-ax
Copy link
Owner

Aras-ax commented Mar 5, 2019

统计所有小于非负整数 n 的质数的数量。

示例:

输入: 10
输出: 4
解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。

解答

一个数如果是质数,那么他的倍数肯定都不是质数

function countPerim(n) {
    let hash = {},
        count = 0;
    for (let i = 2; i < n; i++) {
        if (hash[i]) {
            continue;
        }

        count++;
        for (let j = i; j < n; j = j + i) {
            hash[j] = true;
        }
    }
    return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant