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. 【转帖】k8s-kubectl命令大全

    https://www.cnblogs.com/fuyuteng/p/9458282.html 学习一下 Kubectl命令行管理对象 类型 命令 描述 基础命令 create 通过文件名或标准输入创 ...

  2. DOS ftp

    C:\>ftp /? 将文件传送到运行 FTP 服务器服务(经常称为后台程序)的计算机以及将文件从该计算机传出.可以交互使用 Ftp. FTP [-v] [-d] [-i] [-n] [-g] ...

  3. 【LOJ】#3044. 「ZJOI2019」Minimax 搜索

    LOJ#3044. 「ZJOI2019」Minimax 搜索 一个菜鸡的50pts暴力 设\(dp[u][j]\)表示\(u\)用\(j\)次操作能使得\(u\)的大小改变的方案数 设每个点的初始答案 ...

  4. ArrayList类的set()方法

    ArrayList类的set()方法用于更新指定位置的内容,若内容是new出来的,则需要调用该set()方法:否则,不需要调用该set()方法,示例如下 User.java public class ...

  5. extern int PASCAL

    表示声明一个变量,这个变量在其他地方已经定义,但是这里因为要使用,所以声明下. 写成下面: extern “C” int PASCAL: 说明PASCAL是在一个C文件下定义的.如果不是在C下就不用加 ...

  6. 第9章:LeetCode--算法:HASH表

    哈希表(Hash table,也叫散列表),关键值K和内容的映射表,通过映射函数实现,hashtable(key,value) 进行查询的时候,就是使用哈希函数将关键码key转换为对应的数组下标,并定 ...

  7. 初遇PHP(一)

    因为想给自己弄一个微信公众号,顺便提升一下自己,所以有了以下内容,本次学习的最终目标是能用php制作套微信公众号,然后转成Java.为什么要这么麻烦呢,其一是买的资料书是php的,其二是顺水推舟刚好可 ...

  8. 24-Perl 数据库连接

    1.Perl 数据库连接本章节我们将为大家介绍 Perl 数据库的连接.Perl 5 中我们可以使用 DBI 模块来连接数据库.DBI 英文全称:Database Independent Interf ...

  9. SQL With AS Expression

    A. Creating a simple common table expression The following example shows the total number of sales o ...

  10. c# winfrom 界面设计

    1.在用DotnetBar的RibbonControl时,界面最大化时,会把电脑桌面的任务栏遮盖住: 解决办法:在load事件中写入: , Screen.PrimaryScreen.WorkingAr ...