余数定理

by Anuj Pahade

由Anuj Pahade

如何用Java实现余数定理 (How to implement the Chinese Remainder Theorem in Java)

This post assumes that you know what Chinese Remainder Theorem (CRT) is and focuses on its implementation in Java. If you don’t, I’d recommend you read about it here.

这篇文章假定您知道什么是中文余数定理(CRT),并将重点介绍其在Java中的实现。 如果您不喜欢的话,建议您在这里阅读。

You can find the link to the complete code at the end of this post. So let’s get started.

您可以在本文末尾找到完整代码的链接。 因此,让我们开始吧。

我们需要找到什么? (What do we need to find?)

We need to find X. ?

我们需要找到X。

The statement goes as follows:

该语句如下:

There exists a minimum positive number X such that:

存在一个最小的正数 X,使得:

X % number[0]    =  remainder[0], X % number[1]    =  remainder[1], ...............X % number[k-1]  =  remainder[k-1]

So, here we have two arrays.

因此,这里有两个数组。

  1. The array of numbers: All the numbers in this array are pairwise relatively prime. Which means, pick any two numbers from the array, you’ll find that their greatest common divisor is 1.

    数字数组:此数组中的所有数字都是成对的相对质数。 这意味着,从数组中选择任意两个数字,您会发现它们的最大公约数是1。

  2. The array of remainders: As you can see in the expressions above, when X is divided by a number n from the number array it leaves a respective remainder from the remainder array.

    余数数组:如您在上面的表达式中所看到的,当X用数字数组中的数字n进行除数时,它会从 数组中留下相应的余

实施CRT的步骤 (Steps to implement the CRT)

These are the steps, or as we engineers say, the ‘algorithm’, to implement CRT.

这些是实施CRT的步骤,或者正如我们的工程师所说的“算法”。

步骤1:在第一个数组中找到所有数字的乘积。 (Step 1: Find the product of all the numbers in the first array.)

for(int i=0; i<number.length; i++ ){   product *= number[i];}

步骤2:找出每个数字的偏积。 (Step 2: Find the partial product of each number.)

Partial product of n= product/n

n的部分乘积= product / n

for(int i=0; i<num.length; i++){   partialProduct[i] = product/number[i];}

3.找到number [i]取模取取part [i]的模数乘法逆。 (3. Find the modular multiplicative inverse of number[i] modulo partialProduct[i].)

Here we find the inverse using the extended Euclidean algorithm. So, we call computeInverse(partialProduct[i],num[i])

在这里,我们使用扩展的欧几里得算法求逆。 因此,我们将其称为computeInverse(partialProduct [i],num [i])

public static int computeInverse(int a, int b){         int m = b, t, q;         int x = 0, y = 1;               if (b == 1)             return 0;               // Apply extended Euclid Algorithm         while (a > 1)         {             // q is quotient             q = a / b;             t = b;                   // now proceed same as Euclid's algorithm             b = a % b;a = t;             t = x;             x = y - q * x;             y = t;         }               // Make x1 positive         if (y < 0)          y += m;               return y;     }

步骤4:最终总和 (Step 4: Final Sum)

sum += partialProduct[i] * inverse[i] * rem[i];

步骤5:传回最小的X (Step 5: Return the smallest X)

In order to find the smallest of all solutions, we divide the sum from step 4 by the product from step 2.

为了找到所有解决方案中最小的一个,我们将步骤4的总和除以步骤2的乘积。

return sum % product;

Thus, we have found our X. I’d recommend you to try to implement the code on your own before looking at the code in the link below.

因此,我们找到了X。建议您在查看下面链接中的代码之前,先尝试自行实现代码。

Thanks for reading the post. I hope it helped you. Leave suggestions in the comments below or reach out to me with a better version of this code or queries on anujp5678[at]gmail[dot]com or connect with me on LinkedIn.

感谢您阅读这篇文章。 希望对您有所帮助。 在下面的评论中留下建议,或通过更好的代码版本或在anujp5678 [at] gmail [dot] com上与我联系,或在LinkedIn上与我联系 。

Please clap. ? Claps motivate.

请拍手。 ? 鼓掌激励。

Have fun and happy coding! :)

祝您编程愉快,愉快! :)

翻译自: https://www.freecodecamp.org/news/how-to-implement-the-chinese-remainder-theorem-in-java-db88a3f1ffe0/

余数定理

余数定理_如何用Java实现余数定理相关推荐

  1. preparestatement方法用多次_如何用java 5分钟实现一个最简单的mysql代理服务器?

    用java8基于vert.x3 快速实现一个最简单的mysql代理服务器,只需要5分钟时间. 什么是mysql 代理? mysql代理是介于client端和mysql服务端中间层服务,如下图所示: 这 ...

  2. 如何用命令行写java程序_如何用java实现doc命令行

    如何用java实现doc命令行, dir显示当前所有目录下的文件 cd 文件目录 进入到该目录 cd ..退到上级目录,一面是我写的代码,调试了. 不过有点小bug就是当你进去的是文件就会提示空指针异 ...

  3. java判断那个时间更晚_如何用Java判断日期是早于还是晚于另一个日期

    如何用Java判断日期是早于还是晚于另一个日期 另一个工作中常见的操作就是如何判断给定的一个日期是大于某天还是小于某天?在Java 8中,LocalDate类有两类方法isBefore()和isAft ...

  4. java 判断手机运营商_如何用java判断手机号运营商?

    如何用java实现判断手机号的运营商?因为每个号段都是工信部规定划分给指定运营商的,所以我们可以通过手机号码的号段来判断. 现在手机号的号段那么多,要怎样方便的的判断呢?于是我们就想到了正则表达式,在 ...

  5. java 判断手机号_如何用java判断手机号运营商?

    如何用java实现判断手机号的运营商?因为每个号段都是工信部规定划分给指定运营商的,所以我们可以通过手机号码的号段来判断. 现在手机号的号段那么多,要怎样方便的的判断呢?于是我们就想到了正则表达式,在 ...

  6. java 网络爬虫_如何用Java实现网络爬虫

    原标题:如何用Java实现网络爬虫 微信公众号"书圈"后台回复[Javapachong1],下载本例的PPT和源码 作品描述 本章作品是一个能够抓取指定网站ACM比赛信息的爬虫.A ...

  7. java中循环输入_如何用java循环输入并且当输入0时结束循环?

    如何用java循环输入并且当输入0时结束循环? import java.util.Scanner; public class Avg { public static void main(String[ ...

  8. python java 爬数据_如何用java爬虫爬取网页上的数据

    当我们使用浏览器处理网页的时候,有时候是不需要浏览的,例如使用PhantomJS适用于无头浏览器,进行爬取网页数据操作.最近在进行java爬虫学习的小伙伴们有没有想过如何爬取js生成的网络页面吗?别急 ...

  9. java hdfs 新建目录_如何用java在hdfs中创建一个新目录?

    public static void main(String[] args) throws IOException, URISyntaxException { ????????????配置config ...

最新文章

  1. 机器学习中有哪些形式简单却很巧妙的 idea?
  2. GMP:了解GMF引擎功能Graphical Modeling Framework
  3. DLL中传递STL参数(如Vector或者list等)会遇到的问题[转载]
  4. CentoS8 Mysql8 数据目录迁移
  5. mysql bigint转string_无语了,直到今天,我才揪出MySQL磁盘消耗迅猛的“真凶”!...
  6. cfb为什么不需要填充_学日语为什么不需要准备,现在就可以学?
  7. OJ1067: 有问题的里程表(C语言)
  8. openstack 重启mysql_openstack 重启服务命令
  9. Rendering Path
  10. 时区的概念定义和发展史
  11. 高通平台 Camera基础
  12. Python基于迁移学习的交通信号识别实战【图像多分类任务】【实测准确度超过96.7%】
  13. 人工智能-10种机器学习常见算法
  14. 门电路逻辑符号大全(三态门,同或门,异或门,或非门,与或非门, 传输门,全加器,半加器等)
  15. 映美精两个相机同步采集求助
  16. Vscode python Code Runner中文乱码
  17. 1、请简要介绍下SVM。
  18. 小程序中关于红包雨的实现
  19. getElementById 用法的一个技巧
  20. 零基础量化交易:Python入门

热门文章

  1. 使用mybatis一次性添加多条数据 在oracle 数据库上
  2. HashSet中的add()方法( 一 )(详尽版)
  3. 文字超过省略_从楚篆到楚玺的文字结构
  4. python捕获异常后处理_python异常捕获处理
  5. php 2 往数据库添加数据
  6. 2-Runtime objc_object objc_class
  7. iOS:一句代码实现文本输入的限制
  8. 实时智能决策引擎在蚂蚁金服风险管理中的实践
  9. canvas.width和canvas.style.width区别以及应用
  10. SQLServer查看存储过程的方法