1 Star 0 Fork 2

原罪/javascript-algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Sieve of Eratosthenes

The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to some limit n.

It is attributed to Eratosthenes of Cyrene, an ancient Greek mathematician.

How it works

  1. Create a boolean array of n + 1 positions (to represent the numbers 0 through n)
  2. Set positions 0 and 1 to false, and the rest to true
  3. Start at position p = 2 (the first prime number)
  4. Mark as false all the multiples of p (that is, positions 2 * p, 3 * p, 4 * p... until you reach the end of the array)
  5. Find the first position greater than p that is true in the array. If there is no such position, stop. Otherwise, let p equal this new number (which is the next prime), and repeat from step 4

When the algorithm terminates, the numbers remaining true in the array are all the primes below n.

An improvement of this algorithm is, in step 4, start marking multiples of p from p * p, and not from 2 * p. The reason why this works is because, at that point, smaller multiples of p will have already been marked false.

Example

Sieve

Complexity

The algorithm has a complexity of O(n log(log n)).

References

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wzhgitee/javascript-algorithms.git
git@gitee.com:wzhgitee/javascript-algorithms.git
wzhgitee
javascript-algorithms
javascript-algorithms
master

搜索帮助