Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

  • Hash 哈希

  • String 字符串

  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

    • ClimbingStairs [爬楼梯]
    • CoinChange [钱币兑换]
    • EditDistance [编辑距离]
    • FibonacciNumber [斐波那契数]
    • FindMonthCalendar [月历]
    • KadaneAlgo [最大连续子数组和之Kadane算法]
    • LevenshteinDistance [莱文斯坦距离]
    • LongestCommonSubsequence [最长公共子序列]
    • LongestIncreasingSubsequence [最长递增子序列]
    • LongestPalindromicSubsequence [最长回文子序列]
    • LongestValidParentheses [最长合法括号]
    • MaxNonAdjacentSum [最大非连接子集和]
    • MaxProductOfThree [最大三数积]
    • MinimumCostPath [最小代价路径]
    • NumberOfSubsetEqualToGivenSum [等和子集]
    • RodCutting [棒材切割问题]
    • Shuf [随机样本]
    • SieveOfEratosthenes [埃拉托斯特尼筛法]
      • 介绍
      • 原理
      • 实现
      • 参考
    • SlidingWindow [滑窗]
    • SudokuSolver [数独]
    • TrappingRainWater [接住雨水]
    • TribonacciNumber [翠波那契数]
    • ZeroOneKnapsack [零一背包]
  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • DynamicProgramming 动态规划
jonsam
2022-09-26
目录

SieveOfEratosthenes [埃拉托斯特尼筛法]

# 介绍

给定一个数 n,打印出所有小于或等于 n 的素数。

当 n 小于 1000 万左右时,埃拉托斯特尼筛法是寻找所有小于 n 的素数的最有效方法之一(参考埃拉托斯特尼筛法 - Wikiwand (opens new window))。

埃拉托斯特尼筛法(英语:sieve of Eratosthenes ),简称埃氏筛,也称素数筛。这是一种简单且历史悠久的筛法,用来找出一定范围内所有的质数。所使用的原理是从 2 开始,将每个质数的各个倍数,标记成合数。一个质数的各个倍数,是一个差为此质数本身的等差数列。此为这个筛法和试除法不同的关键之处,后者是以质数来测试每个待测数能否被整除。

# 原理

给出要筛数值的范围 n,找出n)\sqrt{n})n​) 以内的素数p1,p2,…,pkp1,p2,\dots ,pkp1,p2,…,pk。先用 2 去筛,即把 2 留下,把 2 的倍数剔除掉;再用下一个质数,也就是 3 筛,把 3 留下,把 3 的倍数剔除掉;接下去用下一个质数 5 筛,把 5 留下,把 5 的倍数剔除掉;不断重复下去......。

image

# 实现

# JavaScript

/**
 * @function SieveOfEratosthenes
 * @description Calculates prime numbers(素数) till input number n
 * @param {Number} n - The input integer
 * @return {Number[]} List of Primes till n.
 * @see [Sieve_of_Eratosthenes](https://www.geeksforgeeks.org/sieve-of-eratosthenes/)
 */
function sieveOfEratosthenes (n) {
  if (n <= 1) return []
  const primes = new Array(n + 1).fill(true) // set all as true initially
  primes[0] = primes[1] = false // Handling case for 0 and 1
  for (let i = 2; i * i <= n; i++) {
    if (primes[i]) {
      for (let j = i * i; j <= n; j += i) primes[j] = false
    }
  }

  return primes.reduce((result, isPrime, index) => {
    if (isPrime) result.push(index)
    return result
  }, [])
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 参考

  • Sieve of Eratosthenes - Wikiwand (opens new window)
  • 埃拉托斯特尼筛法 - Wikiwand (opens new window)
编辑 (opens new window)
上次更新: 2022/10/27, 20:28:55
Shuf [随机样本]
SlidingWindow [滑窗]

← Shuf [随机样本] SlidingWindow [滑窗]→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式