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. SQLServer学习之表的操作

    SQLServer学习之表的操作 关系数据库通常包含多个表.数据库实际上是表的集合,数据库的数据或者信息都是存储在表中的.表是对数据进行存储和操作的一种逻辑结构,每一个表都代表一个对用户意义的对象. ...

  2. 题解 P3627 【[APIO2009]抢掠计划】

    咕了四个小时整整一晚上 P3627 [APIO2009] 抢掠计划(https://www.luogu.org/problemnew/show/P3627) 不难看出答案即为该有向图的最长链长度(允许 ...

  3. Spring 配置文件注入

    一.Spring配置文件注入 package com.zxguan.demo; public class Person { private String name; private int age; ...

  4. tracert命令详解_tracert结果详解_tracert命令使用详解

    17:06:40 正在等待客服售后工程师令狐冲接入,您可以先简单描述所要咨询的问题,如果长时间没有响应,您也可以 重新选择客服 . 17:06:42 您好,客服售后工程师令狐冲为您服务.售后工程师令狐 ...

  5. C#中操作单个cookie和cookie字典

    单个cookie和cookie字典在浏览器中的存储格式如下:可以看到,单个cookie是以单一键值对的方式存储的,而cookie字典的值包含多个键值对,这些键值对之间以&符号拼接.cookie ...

  6. git 查看当前仓库地址以及设置新的仓库地址

    1.查看当前仓库地址 git remote show origin 2.设置新的仓库地址 1.先登录 gitlab 查看当前仓库地址: 执行修改地址命令 git remote set-url orig ...

  7. wpf menuitem 简约显示的 template样式

    <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}&qu ...

  8. SSH远程连接工具汇总

    1)Xshell 常见问题: 1) 终端中的字体横向显示 字体中带有@的均为横向字体, 只要选择一个不带@的字体即可 2)putty 常见问题: 1)putty中编辑脚本,文字呈现蓝色,辨识度较差,需 ...

  9. C# 获取当前执行DLL 所在路径

    有的时候,当前执行的DLL 和启动的EXE 所在路径并不一致,这时我们想要获得当前执行DLL 所在路径可以使用下面的方法. // Summary: // Gets the path or UNC lo ...

  10. PKGSRC

    PKGSRC简介 pkgsrc: The NetBSD Packages Collection The NetBSD Packages Collection (pkgsrc) 是在NetBSD系统以及 ...