Goldbach //Java大数素数做法 素数判定Miller_Rabin

  1. Goldbach

Description:

Goldbach's conjecture is one of the oldest and best-known unsolved problems in number theory and all of mathematics. It states:

Every even integer greater than 2 can be expressed as the sum of two primes.

The actual verification of the Goldbach conjecture shows that even numbers below at least 1e14 can be expressed as a sum of two prime numbers. 

Many times, there are more than one way to represent even numbers as two prime numbers. 

For example, 18=5+13=7+11, 64=3+61=5+59=11+53=17+47=23+41, etc.

Now this problem is asking you to divide a postive even integer n (2 into two prime numbers.

Although a certain scope of the problem has not been strictly proved the correctness of Goldbach's conjecture, we still hope that you can solve it. 

If you find that an even number of Goldbach conjectures are not true, then this question will be wrong, but we would like to congratulate you on solving this math problem that has plagued humanity for hundreds of years.

Input:

The first line of input is a T means the number of the cases.

Next T lines, each line is a postive even integer n (2

Output:

The output is also T lines, each line is two number we asked for.

T is about 100.

本题答案不唯一,符合要求的答均正确

样例输入

1
8

样例输出

3 5

题目来源

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛


通过这一段代码,

1.领略到Java大数素数做法:用BigInteger中的isProbablePrime(int certainty)函数,用来判断素数,(如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2*certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。

2.在做素数的题目时,可以通过找数据的中间值,如果中间值为偶数的话就加1,这样保证找到的第一个数据时素数,然后用原来的数据减去找到的素数,就可以判断其差是否是素数,这样来查找元素的两素数

import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String args[]){
	Scanner scan=new Scanner(System.in);
    long t=scan.nextLong();
    	for(int i=0;i0){
    		long z=x-y;
    	   if(BigInteger.valueOf(y).isProbablePrime(10) && BigInteger.valueOf(z).isProbablePrime(10))
    	   {
    		   if(BigInteger.valueOf(y).compareTo(BigInteger.valueOf(z))<0)
    		   System.out.println(y+" "+z);
    		   else
    		   System.out.println(z+" "+y);
    		   break;
    	   }
    	    y=y-2;//在保证y始终都是素数
    		}
    	}
    	
   }
}

第二种做法:

素数判定Miller_Rabin

https://blog.csdn.net/fisher_jiang/article/details/986654

你可能感兴趣的:(ACM--大数问题&&高精度,素数,ACM——宁夏网络赛)