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 ...
随机推荐
- Python 爬虫-抓取中小企业股份转让系统公司公告的链接并下载
系统运行系统:MAC 用到的python库:selenium.phantomjs等 由于中小企业股份转让系统网页使用了javasvript,无法用传统的requests.BeautifulSoup库获 ...
- Hadoop2.X主要模块默认端口及作用
Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集记录如 ...
- iOS开发UIResponder简介API
#import <Foundation/Foundation.h> #import <UIKit/UIKitDefines.h> #import <UIKit/UIEve ...
- 【POJ】3259 Wormholes
题目链接:http://poj.org/problem?id=3259 题意:n个农场,m条双向路径,w条单向路径(虫洞).单向虫洞路径是负值.农夫想知道自己能不能看到自己(X). 题解:其实刚开始没 ...
- jdbc_mysql----interset
- 不同JDK版本之间的intern()方法的区别-JDK6 VS JDK6+
String s = new Stirng(“a”); s.intern(); JDK6:当调用intern()方法时,如果字符串常量池先前已创建出该字符串对象,则返回池中的该字符串的引用.否则,将此 ...
- abstract类中method
一.abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized? 都不可以,因为abstract申明的方法是要求子类去实现的,abstrac ...
- python2x 安装 psutil
安装psutil模块: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz --no-check-cer ...
- windows2012 日志查看过程
Windows2012界面修改好造成有些人不知道在哪里查找windows 日志 我这边截图描述一下 1. 2.输入 命令 eventvwr.msc 3.弹出 windows 事件查看器 4.若需要 ...
- openwrt xfrp移植
对开源软件表示支持 https://github.com/KunTengRom/xfrp 上传编译,选择 cp .config xxx make 刷机 客户端配置文件: /tmp/etc# cat x ...