[Algorithm] Finding Prime numbers - Sieve of Eratosthenes
Given a number N, the output should be the all the prime numbers which is less than N.
The solution is calledSieve of Eratosthenes:
First of all, we assume all the number from 2 to N are prime number (0 & 1 is not Prime number).
According to the Primse number defination that Prime number can only be divided by 1 & itself. So what we do is start from
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
...
2 * j <= N
3 * 2 = 6
3 * 3 = 9
...
i * j <= N
i is from 2 to N.
We are going to mark all the caluclated number to be Not prime numbers. In the end, the remining numbers should be Primes.
function findPrime (n) {
let primes = [];
for (let i = 0; i <= n; i++) {
primes.push(1);
}
primes[0] = 0;
primes[1] = 0;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (primes[i] === 1) {
for (let j = 2; i * j <= n; j++) {
primes[i * j] = 0;
}
}
}
return primes.map((val, index) => val === 1 ? index: 0).filter(Boolean);
}
findPrime(14) // [ 2, 3, 5, 7, 11, 13 ]
One optimization, we don't need to loop i from 2 to N, it is enough from 2 to Math.sqrt(n)
[Algorithm] Finding Prime numbers - Sieve of Eratosthenes的更多相关文章
- CSU 2018年12月月赛 F(2218): Finding prime numbers
Description xrdog has a number set. There are 95 numbers in this set. They all have something in com ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- 使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp)
Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime ...
- poj 2379 Sum of Consecutive Prime Numbers
...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】
拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- Alexandra and Prime Numbers(思维)
Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- How many prime numbers(求素数个数)
How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- Centos7 + nginx 托管 Django 项目
使用nginx托管django服务的原理 使用uwsgi开启django服务(通过配置文件启动) 防火墙关闭uwsgi端口(uwsgi的websocket一定要使用127.0.0.1的方式配置)) 编 ...
- 详解mysql复制机制--异步复制,半同步复制和并行复制
图4 那么如何并行化,并行IO线程,还是并行SQL线程?其实两方面都可以并行,但是并行SQL线程的收益更大,因为SQL线程做的事情更多(解析,执行).并行IO线程,可以将从Master拉取和写Rela ...
- localStorage 杂记
localStorage html5标准 Web 存储现在的主流浏览器,包括IE 8+.Chrome 4+.Firefox 3.5+.Opera 10.5+.Safari 4+.iPhone 2+.A ...
- 从入门到自闭之Python--MySQL数据库的单表操作
单表查询:select * from 表 where 条件 group by 分组 having 过滤 order by 排序 limit n; 语法: select distinct 字段1,字段2 ...
- Codeforces 1240C. Paint the Tree
传送门 首先每个点 $u$ 只能选择不超过 $k$ 个相连的边 并且设边为 $(u,v)$ ,那么此时 $v$ 也必须选择这条边 因为图是一颗树,显然考虑一下树形 $dp$ 设 $f[x][0/1]$ ...
- CentOS 中利用docker安装MySQL
1.前提条件 centos7 且内核版本高于3.10, 可通过命令: uname -r 查看内核版本 2.利用yum 安装docker 安装一些必要的系统工具: sudo yum install -y ...
- Bootstrap3基础教程 01 概述
移动设备优先是 Bootstrap 3 的最显著的变化. 基础的页面: <!DOCTYPE html> <html> <head> <meta charset ...
- github常用搜索技巧
1.在项目名称,readme文件和描述中包含关键字seckill的项目seckill in:name,readme,description 2.fork大于500,stars大于500springbo ...
- Groovy--使用模板引擎和GroovyShell执行插值字符串
package curveJudge import groovy.text.SimpleTemplateEngine /** * Created by Jxy on 2019/8/26 17:16 * ...
- SQL Tuning 基础概述10
在<SQL Tuning 基础概述05 - Oracle 索引类型及介绍>的1.5小节,提到了几种"索引的常见执行计划": INDEX FULL SCAN:索引的全扫描 ...