62,628
社区成员
发帖
与我相关
我的任务
分享package com.meisen;
import java.math.BigInteger;
/**
* @authorADMIN 创建时间:2020/4/13
*/
public class MeiSen {
//不断产生素数n
//判断2^n-1是不是素数,是就拿出来
public boolean isSuShu(BigInteger num) {//传进来一个BigInteger类型的num
if (num.compareTo(new BigInteger("2")) == 0 ? false : true) {//如果num等于2
for (BigInteger i = new BigInteger("2"); i.compareTo(num) < 0 ? true : false; i = i.add(new BigInteger("1"))) {
if (num.mod(i).compareTo(new BigInteger("0")) == 0) {//这个数对2到num-1取模,一旦为0
return false;//返回false,num不是素数
}
}
}
return true;
}
public void meiSen() {
BigInteger bigInteger = new BigInteger("2");
while (true) {
//判断bigInteger是不是素数
if (isSuShu(bigInteger)) {//此时bigInteger是素数,然后计算2的bigInteger次方减一,并判断是否为素数
//计算2的bigInteger次方
//System.out.println("指数n------------------------------------------------> "+bigInteger.toString());
BigInteger bI1 = new BigInteger("2");
for (BigInteger i = new BigInteger("1"); i.compareTo(bigInteger) < 0 ? true : false; i = i.add(new BigInteger("1"))) {
bI1 = bI1.multiply(new BigInteger("2"));
}
//再减一
bI1 = bI1.subtract(new BigInteger("1"));
//判断2^n-1整个是否为素数
long start = System.currentTimeMillis();
if (isSuShu(bI1)) {
System.out.println(bI1.toString() + " 耗时:" + (System.currentTimeMillis() - start) + "ms");
}
}
//bigInteger不是素数
bigInteger = bigInteger.add(new BigInteger("1"));
}
}
public static void main(String[] args) {
MeiSen meiSen = new MeiSen();
meiSen.meiSen();
}
}
public class Sample {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
System.out.printf("n=%d, 2^n-1=%d\n", 2, (1<<2)-1);
for (int n=3; n<5000; n+=2) { //n在5000以内
if (isPNum1(n)) {
if (n < 32) { //int范围内
if (isPNum1((1<<n)-1)) {
System.out.printf("n=%d, 2^n-1=%d\n", n, (1<<n) - 1);
}
//} else if (n < 64) { //long范围内
// if (isPNum2((1L<<n) - 1)) {
// System.out.printf("n=%d, 2^n-1=%d\n", n, (1L<<n) - 1);
// }
} else { //上记范围外
BigInteger b = BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE);
if (isPNum3(b)) {
System.out.printf("n=%d, 2^n-1=%s\n", n, b.toString());
}
}
}
}
long t2 = System.currentTimeMillis();
System.out.printf("耗时 %d 毫秒\n", (t2-t1));
}
public static boolean isPNum1(int num) {
if (num==2) return true;
else if (num<2 || num%2==0) return false;
for (int i = 3; i <= Math.sqrt(num); i++)
if (num % i == 0) {
return false;
}
return num>1;
}
public static boolean isPNum2(long num) {
if (num==2) return true;
else if (num<2 || num%2==0) return false;
for (long i = 3; i <= Math.sqrt(num); i++)
if (num % i == 0) {
return false;
}
return true;
}
public static boolean isPNum3(BigInteger b) {
/*
BigInteger two = BigInteger.valueOf(2);
if (b.compareTo(two) == 0) return true;
else if (b.compareTo(two) < 0 || b.mod(two).compareTo(BigInteger.ZERO) == 0) return false;
BigInteger three = BigInteger.valueOf(3);
for (BigInteger i=three; i.compareTo(b.divide(three))<0; i=i.add(two)) {
if (b.mod(i).compareTo(BigInteger.ZERO) == 0) {
return false;
}
}
return true;
*/
return b.isProbablePrime(Integer.MAX_VALUE); //BigInteger自带判断是否是素数的方法
}
}
[/quote]
你这个判断素数的方法确实挺快,比我的快太多了,已结贴给分,感谢

public class Sample {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
System.out.printf("n=%d, 2^n-1=%d\n", 2, (1<<2)-1);
for (int n=3; n<5000; n+=2) { //n在5000以内
if (isPNum1(n)) {
if (n < 32) { //int范围内
if (isPNum1((1<<n)-1)) {
System.out.printf("n=%d, 2^n-1=%d\n", n, (1<<n) - 1);
}
//} else if (n < 64) { //long范围内
// if (isPNum2((1L<<n) - 1)) {
// System.out.printf("n=%d, 2^n-1=%d\n", n, (1L<<n) - 1);
// }
} else { //上记范围外
BigInteger b = BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE);
if (isPNum3(b)) {
System.out.printf("n=%d, 2^n-1=%s\n", n, b.toString());
}
}
}
}
long t2 = System.currentTimeMillis();
System.out.printf("耗时 %d 毫秒\n", (t2-t1));
}
public static boolean isPNum1(int num) {
if (num==2) return true;
else if (num<2 || num%2==0) return false;
for (int i = 3; i <= Math.sqrt(num); i++)
if (num % i == 0) {
return false;
}
return num>1;
}
public static boolean isPNum2(long num) {
if (num==2) return true;
else if (num<2 || num%2==0) return false;
for (long i = 3; i <= Math.sqrt(num); i++)
if (num % i == 0) {
return false;
}
return true;
}
public static boolean isPNum3(BigInteger b) {
/*
BigInteger two = BigInteger.valueOf(2);
if (b.compareTo(two) == 0) return true;
else if (b.compareTo(two) < 0 || b.mod(two).compareTo(BigInteger.ZERO) == 0) return false;
BigInteger three = BigInteger.valueOf(3);
for (BigInteger i=three; i.compareTo(b.divide(three))<0; i=i.add(two)) {
if (b.mod(i).compareTo(BigInteger.ZERO) == 0) {
return false;
}
}
return true;
*/
return b.isProbablePrime(Integer.MAX_VALUE); //BigInteger自带判断是否是素数的方法
}
}

public class Sample {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
for (int n=0; n<100; n++) { //可以调整循环范围
if (isPNum(n)) {
if (n < 32) { //int范围内
System.out.printf("n=%d, 2^n-1=%d\n", n, (1<<n) - 1);
} else if (n < 64) { //long范围内
System.out.printf("n=%d, 2^n-1=%d\n", n, (1L<<n) - 1);
} else { //上记范围外
BigDecimal b = new BigDecimal(2);
b = b.pow(n);
b = b.subtract(BigDecimal.ONE);
System.out.printf("n=%d, 2^n-1=%s\n", n, b.toString());
}
}
}
long t2 = System.currentTimeMillis();
System.out.printf("耗时 %d 毫秒\n", (t2-t1));
}
public static boolean isPNum(int num) {
if (num==2) return true;
else if (num<2 || num%2==0) return false;
int tmp = num, n = 0;
while (tmp>0) { //算出n
n++;
tmp >>= 1;
}
if (num==(1<<n)-1) {
return isPNum(n); //利用规律递归,判断num是否符合2的n次方减1
}
for (int i=3; i<=(1<<((n+1)/2)); i++) { //走到这里必然是奇数,所以for从3开始即可
if (num%i==0) return false;
}
return true;
}
public static boolean isPNum2(int num) {
for (int i = 2; i <= Math.sqrt(num); i++)
if (num % i == 0)
return false;
return num>1;
}
}