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 ...
随机推荐
- Spring源码由浅入深系列四 创建BeanFactory
继上一章refresh之后,上图描述了obtainFreshBeanFactory过程.
- PAT_A1079#Total Sales of Supply Chain
Source: PAT A1079 Total Sales of Supply Chain (25 分) Description: A supply chain is a network of ret ...
- pandas 使用dataframe 索引项相同时出现bug
使用的是join函数来合并两个dataframe: df=df2.join(df1) bug:columns overlap but no suffix specified: Index([u'muk ...
- 为什么 TCP 建立连接是三次握手,关闭连接确是四次挥手呢?
Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:小书go https://blog.csdn.net/qzcsu/article/details/72861891 背 ...
- WinCE下的第二个窗口程序
MFC写的,有些简陋,但是还是感觉不错,一个小小的计算器,各个方面的功能都完成了 但是唯独那个CEdit里面的文字不能右对齐.那个扩展风格用不了
- netty 文件传输
FileServer package com.zhaowb.netty.ch13_1; import io.netty.bootstrap.ServerBootstrap; import io.net ...
- 转: PHP中this,self,parent的区别
{一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...
- Flutter 集成到现有iOS工程
前沿 由于我司已经有自己的App,flutter属于技术引进的一部分,也不太可能重新启动一个项目,因此目前我们是将flutter模块形式注入我们的App之中.即:将flutter模块集成到现在有iOS ...
- CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架
<!doctype html> CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架.mdhtml {overflow-x: initia ...
- Algo: Two Sum
类似的题目可以用HashTable的思想解决. 1.Two Sum Given an array of integers, return indices of the two numbers such ...