Goldbach 米勒 罗宾算法

2024-01-31 06:32
文章标签 算法 罗宾 米勒 goldbach

本文主要是介绍Goldbach 米勒 罗宾算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.来源:https://blog.csdn.net/qq_38749759/article/details/80042527

2.算数检测法介绍:https://blog.csdn.net/zengaming/article/details/51867240

3.米勒 罗宾算法原理介绍:https://blog.csdn.net/fisher_jiang/article/details/986654

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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define LL unsigned long long//注意这个longlong的定义,必须有 unsigned
using namespace std;
LL prime[6] = {2, 3, 5, 233, 331};
LL qu(LL x, LL y, LL mod) 
{LL ret = 0;while(y) {if(y & 1)ret = (ret + x) % mod;x = x * 2 % mod;y >>= 1;}return ret;
}
LL qpow(LL a, LL n, LL mod){LL ret = 1;while(n) {if(n & 1) ret = qu(ret, a, mod);a = qu(a, a, mod);n >>= 1;}return ret;
}
bool MR(LL p) 
{if(p < 2) return 0;if(p != 2 && p % 2 == 0) return 0;LL s = p - 1;while(! (s & 1)) s >>= 1;for(int i = 0; i < 5; ++i){if(p == prime[i]) return 1;LL t = s, m = qpow(prime[i], s, p);while(t != p - 1 && m != 1 && m != p - 1) {m = qu(m, m, p);t <<= 1;}if(m != p - 1 && !(t & 1)) return 0;}return 1;
}int main()
{LL p;int t;cin>>t;while (t){cin>>p;if(MR(p-2) ) {cout<<"2"<< " "<<p-2;   continue;}for(LL i=3;i<=p/2;i+=2){if (MR(i) )if(MR(p-i)){cout<<i<<" "<<p-i<<endl;break;}}t--;}return 0;
}package javasishuyan;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {public static void main(String[] args) {Scanner scan=new Scanner(System.in);int num=scan.nextInt();BigInteger a,k;for(int i=0;i<num;i++){a=scan.nextBigInteger();for(k=BigInteger.valueOf(2);k.compareTo(a)<0;k=k.add(BigInteger.ONE)){if(IsSushu(k)&&IsSushu(a.subtract(k))){System.out.println(k+" "+a.subtract(k));break;}}}}public static boolean IsSushu(BigInteger a){if(a.isProbablePrime(10)){return true;}else{return false;}}
}1.还是java好理解,现在才知道java还给大数提供了专门的判断素数的方法:isProbablePrime(10)2.还有就是java 中大数累加,用k=k.add(BigInteger.ONE),ONE就表示1的意思。

 

这篇关于Goldbach 米勒 罗宾算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/662859

相关文章

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1