leecode/backlog/204.count-primes.js
2020-05-22 18:17:19 +08:00

59 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @lc app=leetcode id=204 lang=javascript
*
* [204] Count Primes
*
* https://leetcode.com/problems/count-primes/description/
*
* algorithms
* Easy (28.33%)
* Total Accepted: 229.8K
* Total Submissions: 798.7K
* Testcase Example: '10'
*
* Count the number of prime numbers less than a non-negative number, n.
*
* Example:
*
*
* Input: 10
* Output: 4
* Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
*
*
*/
/**
* @param {number} n
* @return {number}
*/
var countPrimes = function(n) {
// tag: 数论
// if (n <= 2) return 0;
// let compositionCount = 0;
// for(let i = 3; i < n; i++) {
// for(let j = i - 1; j > 1 ; j--) {
// if (i % j === 0) {
// compositionCount++;
// break; // 找到一个就可以证明它不是质数了
// }
// }
// }
// return n - compositionCount - 2; // 需要减去1和n这两个数字
// 上面的方法会超时,因此我们需要进行优化
// 数学角度来看如果一个数字可以分解为两个数字相乘这两个数字不包括0和它本身那么它就是合数
const compositions = []; // compositions[i] 表示i是否是合数
let count = 0;
for(let i = 2; i < n; i++) {
if (!compositions[i]) count++;
for(let j = 2; i * j < n; j++) {
compositions[i * j] = true;
}
}
return count;
};