(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的更多相关文章

  1. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. 每日一九度之 题目1040:Prime Number

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  3. 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 ...

  4. 10 001st prime number

    这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...

  5. [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 ...

  6. [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 ...

  7. 10_ for 练习 _ is Prime Number ?

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Java 网络编程(1):使用 NetworkInterface 获得本机在局域网内的 IP 地址

    原文地址:https://segmentfault.com/a/1190000007462741 1.问题提出 在使用 Java 开发网络程序时,有时候我们需要知道本机在局域网中的 IP 地址.很常见 ...

  2. Jsoup 学习笔记

    这里写自定义目录标题 Jsoup 学习笔记 解析 HTML 的字符串解析 URL 解析 本地文件解析 解析数据 DOM 解析 使用选择器解析 选择器概述 选择器组合用法 过滤用法 修改数据 HTML ...

  3. Java 并发工具包——ExecutorService常用线程池

    1. 执行器服务 ExecutorService java.util.concurrent.ExecutorService 接口表示一个异步执行机制,使我们能够在后台执行任务.因此一个 Executo ...

  4. Java异常类及处理

    异常概述:运行时发生的不正常情况 在java中用类的形式对不正常的情况进行了描述和封装对象. 描述不正常的类,称之为异常类. 异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述 ...

  5. 各版本IE兼容问题,IE6,IE7,IE8,IE9,IE10,IE11

    在网站开发和学习中,由于各种兼容性问题,让开发者挺烦恼的,我的学员也经常因为兼容问题来找我取经. 事实上,IE给出了解决方案,谷歌给出了解决方案,国内著名网站百度也将这个解决方案应用于IE的兼容性问题 ...

  6. ie8以下不兼容h5新标签的解决方法

    HTML5新添了一些语义化标签,他们能让代码语义化更直观易懂,有利于SEO优化.但是此HTML5新标签在IE6/IE7/IE8上并不能识别,需要进行JavaScript处理. 解决思路就是用js创建h ...

  7. iOS开发系列-HTTPS

    HTTPS 网景在1994年创建了HTTPS,并应用在网景导航者浏览器中. 最初,HTTPS是与SSL一起使用的:在SSL逐渐演变到TLS. HTTPS协议与HTTP协议的一些不同: http是超文本 ...

  8. SVN 分支操作

    一  拉取分支 1 选择浏览 2 输入svn项目路径:https://IP/svn/ 3 选择拉取的项目 4 下载到本地路劲 右键选中的分支—CheckOut 选择本地路劲 二 分支合并 1 分支合并 ...

  9. 双十一HostGator独立服务器方案

    一年一度的“双十一”购物狂欢节到来,各大电商平台线上消费的各种“吸金”开启了“双十一”模式,一年一度的“双十一”网购狂欢又开始以“巨大的价格优势”来勾起消费者的购买欲望. 此次双十一期间,HostGa ...

  10. Git 如何使用ssh上传或者同步/下载项目到github

    上传本地代码及更新代码到GitHub教程 上传本地代码 第一步:去github上创建自己的Repository,创建页面如下图所示: 红框为新建的仓库的https地址 第二步: echo " ...