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<n<2^63) 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<n<2^63).

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;i<t;i++)
    	{
    		long x=scan.nextLong();
    		if(x==4){
    			System.out.println(2+" "+2);
    			continue;
    		}
    		long y=x/2;//找输入的元素的一半
    		if(y%2==0)//判断是否是偶数
    			y++;//偶数加1后就是奇数,偶数一定不是素数,奇数可能不是素数,排除偶数的可能性
    		while(y>0){
    		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

Java中,你可以编写一个程序来验证这个著名的哥德巴赫猜想(Goldbach's Conjecture),虽然目前数学上尚未得到证明,但大量的数值测试支持这一猜想。下面是一个简单的算法实现思路: 1. 定义一个函数,用于检查一个数是否为质数(素数)。例如,你可以使用埃拉托斯特尼筛法(Sieve of Eratosthenes)来生成一定范围内的所有素数。 ```java boolean isPrime(int num) { if (num <= 1) return false; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return false; } return true; } ``` 2. 接着,对从4开始到给定的大于6的偶数遍历,尝试找到两个素数之和等于这个偶数。可以从2开始,寻找比目标数小的所有素数,每次找到一个素数,检查另一个较小的素数是否满足条件。 ```java void goldbachConjecture(int n) { for (int i = 2; i < n; ++i) { // 从最小的素数开始 if (isPrime(i) && isPrime(n - i)) { System.out.println(n + " 可以表示为 " + i + " 和 " + (n - i) + " 的和,它们都是素数"); break; // 找到一对素数就退出循环 } } } ``` 运行`goldbachConjecture(n)`函数,其中`n`是你想要检验的偶数,如果找不到符合条件的素数对,那么理论上讲,哥德巴赫猜想即成立,因为没有找到反例。 注意:这只是一个简化版的示例,并非严格的数学证明。实际应用中,尤其是对于大数,这种搜索可能非常耗时,需要高效的算法优化或者使用更强大的数据结构如并查集来进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值