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. Redis(1.4)Redis的持久化

    Redis持久化 [1]概念 Redis所有的数据存储在内存中,为了保证重启后,redis数据不丢失,需要把redis数据保存在磁盘中. [2]持久化使用方式策略 (1)RDB 方式:默认支持,不需要 ...

  2. Android的视图(View)组件

    Android的绝大部分UI组件都放在android.widget包及其子包.android,view包及其子包中,Android应用的所有UI组件都继承了View类,View组件非常类似于Swing ...

  3. Ugly Numbers UVA - 136(优先队列+vector)

    Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...

  4. P1417 烹调方案(思维+01背包)

    (点击此处查看原题) 题意 有n种食材,每种食材有三个属性,ai,bi和ci,如果在t时刻完成第i样食材则得到ai-t*bi的美味指数,用第i件食材做饭要花去ci的时间.问在T时间内,什么样的烹调方案 ...

  5. flask项目配置

    config.py: class Config(object): """项目的配置""" DEBUG = True SECRET_KEY = ...

  6. MySQL之主键

    一.主键  primary key (唯一标识 .不能重复.不能为空) 1.主键-----是表中的字段,这个字段能唯一标识一条记录.例如 学生表(学号.姓名,年级)里的学号,不能重复.不能为空: 课程 ...

  7. Sql server 2012 企业中文版安装图文教程

    https://blog.csdn.net/qq_30754565/article/details/82421542

  8. 终身机器学习(Lifelong Machine Learning)综述

    终身机器学习(Lifelong Machine Learning)综述 2015年10月23日 17:34:57 qrlhl 阅读数 7805更多 分类专栏: 机器学习   版权声明:本文为博主原创文 ...

  9. mysql 8.x 登陆提示 Access denied for user 'root'@'localhost' (using password: YES)

    第一步:修改 /etc/mysql/my.cnf. 在[mysql]下添加skip-grant-table:重启. 第二步:通过mysql命令登陆: flush privileges; use mys ...

  10. hashMap,hashTable,TreeMap,concurrentHashMap区别

    hashMap: 基于哈希表实现 treeMap: 基于二叉树实现,适用于排序 hashTable: 底层还是HashMap,在方法上加了同步 concurrentHashMap: java7底层通过 ...