[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 ...
随机推荐
- 深入应用C++ 11 C2
template<typename T> void print(T& t) { cout << "lvalue" << endl; } ...
- java:线上问题排查常用手段(转)
出处:java:线上问题排查常用手段 一.jmap找出占用内存较大的实例 先给个示例代码: import java.util.ArrayList; import java.util.List; imp ...
- Ansible-批量导入key(入门)
系统是centos7.5 python2.75 yum install -y ansible ssh-keygen -t rsa vim /etc/ansible/hosts 定义的一个hello组: ...
- java自定义excel
文件下载 本文主要介绍spring boot环境下,利用Apache POI操作Excel,实现Excel文件的在线下载. 首先,我们在这里介绍一下关于ApachePOI中关于HSSF,XSSF和SX ...
- C#开发微信公众平台-就这么简单(转载)(附原文链接)
一直使用的是一百八的诺鸡鸭,没有想去接触看起来风风火火的移动互联网:但因工作需要维护一个微信公众订阅号,考虑以前有做网站的基础,就想着做个简单的微信后台管理:看了官方的开发文档,比狗哥地图的短许多,又 ...
- 实现div可以调整高度(div实现resize)
实现div可以调整高度(div实现resize) 一.div 实现resize(类似textarea) 代码如下: <!DOCTYPE html> <html> <hea ...
- vue 编译警告 Compiled with 4 warnings
问题原因: windows下盘符的大小写导致的. 我在cmd里运行的时候,是切换到小写,改成大写的E盘符就没问题了
- 1.Linux文件及目录结构
Linux 文件结构 在Linux中 ,一切皆文件 目录结构
- vue+axios请求头封装
import { mapMutations } from 'vuex' import axios from 'axios' import { Toast } from 'mint-ui'; impor ...
- fastadmin 金额 字段类型及html验证
金额 字段类型 整数 小数 decimal 10 2 float 10 2 html验证 <div class="form-group&q ...