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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 使用埃拉托色尼筛选法(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 ...

  4. poj 2379 Sum of Consecutive Prime Numbers

                                                                                                        ...

  5. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  6. [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】

    拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...

  7. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  8. Alexandra and Prime Numbers(思维)

    Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  9. How many prime numbers(求素数个数)

    How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. IDEA中便捷更新Git项目最新代码

    更新:IDEA中直接点击Gir后面的第一个图标 会出现一个这样的弹框,点击OK,就可以将GitLab中最新的代码更新到IDEA中(本地)

  2. SpringBoot起飞系列-配置嵌入式Servlet容器(八)

    一.前言 springboot中默认使用的是tomcat容器,也叫做嵌入式的servlet容器.因为它和我们平常使用的tomcat容器不一样,这个tomcat直接嵌入到的springboot,平常我们 ...

  3. Python 3.8.0 正式版发布,新特性初体验 全面介绍

    Python 3.8.0 正式版发布,新特性初体验 北京时间 10 月 15 日,Python 官方发布了 3.8.0 正式版,该版本较 3.7 版本再次带来了多个非常实用的新特性. 赋值表达式 PE ...

  4. 北电之死:谁谋杀了华为的对手?——银湖资本(Silver Lake)董事总经理爱德华·詹德出任CEO,既不了解华为,也不重视中国,直截了当地否决了收购华为

    作者:戴老板:微信公众号:饭统戴老板(ID: worldofboss) 2003年5月,北京SARS疫情紧张,摩托罗拉集团总裁迈克·扎菲罗夫斯基(Mike Zafirovski)却准备不走寻常路,决定 ...

  5. Java进阶开发-基于Base64的加密与解密操作

    基于Base64的加密与解密操作 正常来讲加密基本上永远伴随着解密,所谓的加密或者解密往往都是需要有一些所谓的规则.在JDK1.8开始提供有一组新的加密处理操作,Base64处理.在这个类里面有两个内 ...

  6. Linux下离线安装Docker最新版本

    一.基础环境1.操作系统:CentOS 7.32.Docker版本:18.06.1 官方下载地址(打不开可能需要梯子)3.百度云Docker 18.06.1地址:https://pan.baidu.c ...

  7. c#获取桌面路径和bin文件的路径

    string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory): 生成的运行bin文件下的路径: ...

  8. JavaScript笔记(1)

    1.JavaScript的基本概念 JavaScript是一个解释型的脚本语言 JavaScript可以写在HTML文档内部的任何地方 行内式 内嵌式 链入式:<script src=" ...

  9. vue项目 时间戳转 格式

    项目用了 element UI的日期插件,修改时 时间回显不了,打印出来是换行了,因此要转换 changeTime(value){ let date = new Date(value); let y ...

  10. MySQL 5.7.18 zip版本的安装使用方法

    转自:https://www.cnblogs.com/nepulgh/p/7152618.html MySQL 5.7.18 zip版本的安装使用方法 这个版本的MySQL不像那种点击就可以立即安装, ...