about how to determine a prime number
(1) if divided by 2 or 3, then no;
(2) we only have to go through prime factors; because a composite can always be divided into primes.
(3) since 2 is the smallest prime, for number N, we only have to go through till N/2 max., because if one number is not a prime, the other factor must be no less than 2;
(4) consider N=n*m. If n<sqrt( N ), then it’s a must that m>sqrt( N ). So we only have to go through till sqrt( N )+1 max., because if there’s not a factor with in [2, sqrt(N)+1], there wouldn’t be one above;
(5) other than 2 and 3, prime numbers trend to have a format of (6n +/- 1), but not vise versa.

Now, I haven’t seen a strick mathematical prove on that theory, but someone has run a promgram certifying that at least the first 1 million prime numbers fit in that conclusion.
So if the number is not insanely big, it’s true.
That being say, if we divide a number by (6n +/- 1), it would include many non-prime dividers of course, but we are able to cover all prime factors, too.
Followed is one example:
l = (int) Math.sqrt (n) + 1;
for (i=6; i<=l; i+=6) {
if (n % (i + 1) == 0) return false;
if (n % (i - 1) == 0) return false;
}
// must be prime
(6) seive of Eratosthenes
https://zh.wikipedia.org/zh-hans/%E5%9F%83%E6%8B%89%E6%89%98%E6%96%AF%E7%89%B9%E5%B0%BC%E7%AD%9B%E6%B3%95
The running time for this algorithm is: O = nlog(logn).A pseudo code as followed:
Input: an integer n > 1 Let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true. for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n :
A[j] := false Output: all i such that A[i] is true.
Use seive of Eratosthenes would greatly improve the screening speed. Followed is one example:
public static void main (String args[]) {
int i, j, l;
A = new boolean[N+1];
// do a sieve of Eratosthenes
for (i=0; i<=N; i++) A[i] = true;
l = (int) Math.sqrt (N);
// for each number i from 2 to square root of N...
for (i=2; i<=l; i++)
// ...mark off all the multiples of i
for (j=i*i; j<=N; j+=i) A[j] = false;
// count whatever is left; these are all the primes
for (i=2,j=0; i<=N; i++) if (A[i]) j++;
System.out.println (j);
}
about how to determine a prime number的更多相关文章
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 每日一九度之 题目1040:Prime Number
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- LintCode-Kth Prime Number.
Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eli ...
- 10 001st prime number
这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- 10_ for 练习 _ is Prime Number ?
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告
题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
随机推荐
- map、filter、forEach、reduce数组方法的封装
1.map方法的封装 Array.prototype.mapAlley = function(callback){ //获取调用mapAlley这个方法的数组 let arr = thi ...
- 创建一个学生表student,默认的表空间为users,字段自定,同时为表的各个字段分别添加合适的约束,然后测试约束的验证状态。
create table student(id number(4) constraint prim_key primary key,name varchar(8) not null,sex varch ...
- Elasticsearch(Transport Client)常用操作
这里描述操作elasticsearch采用TransportClient这种方式,官方明确表示在ES 7.0版本中将弃用TransportClient客户端,且在8.0版本中完全移除它. 记录一些常用 ...
- 鼠标悬浮到div上,div进行360°旋转
<!DOCTYPE html> <html> <head> <title>旋转</title> </head> <styl ...
- overleaf 提交arXiv 不成功
从overleaf下载的PDF不能够直接提交给arXiv,但是可以在submit中选择导出下载压缩包,图片不能是png,最好是PDF 或者eps. 参考文献是bbl 文件,不是bib.
- JS去除字符串拼接末尾的符号(逗号)
前言:在JS中,有时需要进行字符串的拼接,而拼接的字符串以某种符号分隔,在拼接好的字符串末尾会多出一个分隔符,这时我们可以采取以下几种方法来去除末尾的分隔符. 方法一:使用substring() 和 ...
- try install gitlab ce at docker ce
sudo docker run --detach \ --env GITLAB_OMNIBUS_CONFIG="external_url 'http://192.168.1.30:8087/ ...
- java运行字符串代码
本文链接:https://blog.csdn.net/junlong750/article/details/50945883
- duilib教程之duilib入门简明教程7.XML基础类
现在大家应该对XML描述界面不那么陌生了,那么我们做进一步介绍. 前面的教程我们写了很多代码,为的是让大家了解下基本流程,其实duilib已经对常用的操作做了很好的包装,正式使用时无需像前面的教程那样 ...
- Linux课程---15、域名相关
Linux课程---15.域名相关 一.总结 一句话总结: 先购买域名,再备案,再解析,域名即可使用 1.域名备案是怎么回事(比如二级域名,三级域名)? 每个二级域名需要备案一次,三级域名不需要备案, ...