[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 ...
随机推荐
- 【LOJ】#3046. 「ZJOI2019」语言
LOJ#3046. 「ZJOI2019」语言 先orz zsy吧 有一个\(n\log^3n\)的做法是把树链剖分后,形成logn个区间,这些区间两两搭配可以获得一个矩形,求矩形面积并 然后就是对于一 ...
- 红米K20PRO解锁Bootloader权限并刷入recovery
手机里反正没什么东西了,聊天记录啊好像也没很重要得了,索性全部清除,刷机玩玩. 把稳定版刷成第三方开发版,这样又有时间去折腾root权限,面具和xposed的各种插件了,嘿嘿. 解锁小米手机 我的账号 ...
- SOSdp
layout: post title: SOSdp author: "luowentaoaa" catalog: true tags: mathjax: true - codefo ...
- JDK1.8新特性(二):Collectors收集器类
一. 什么是Collectors? Java 8 API添加了一个新的抽象称为流Stream,我们借助Stream API可以很方便的操作流对象. Stream中有两个方法collect和collec ...
- 修改hosts文件 解决coursera可以登录但无法播放视频的问题
我们经常为了学习或者了解一些领域的知识为访问国外的网站,但是在国内,很多优秀的网站都被封锁了.在GFW(墙)的几种封锁方式中,有一种就是DNS污染,GFW会对域名解析过程进行干扰,使得某些被干扰的域名 ...
- Scala学习十——特质
一.本章要点 类可以实现任意数量的特质 特质可以要求实现它们的类具备特定的字段,方法或超类 和Java接口不同,Scala特质可以提供方法和字段实现 当你将多个特质叠加在一起时,顺序很重要——其方法先 ...
- C# Reflection exception Method not found
C# Reflection exception Method not found Ok I figured it out. The server has Microsft .NET 4.0 insta ...
- 在django中部署vue项目,不单独抽离dist文件
1,在django项目下(app所在目录),新建vue项目,使用脚手架构建vue项目,vue create (项目名) 2,构建好以后,配置django: (1),配置settings: · 修改te ...
- PHP之配置
1) 错误日志 一.相关配置 需要将php.ini中的配置指令做如下修改: . error_reporting = E_ALL ;将会向PHP报告发生的每个错误 . display_errors = ...
- CSS3溢出文字省略
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...