简体   繁体   English

需要平方根的算法提供“余数”

[英]Need an algorithm for square root that provides a “remainder”

I'm writing a calculator without using decimals (supports only Rational numbers), but I'd like to be able to do a version of square root. 我在编写不使用小数的计算器(仅支持有理数),但是我希望能够做平方根的版本。

When a square root function is pressed for (say) the number 12, I'd like to just simplify/"reduce" the square root and return 2*sqrt(3)--by it into (2*2) * 3 and extracting the sqrt(2*2) as 2. 当对(例如)数字12按下平方根函数时,我只想简化/“减少”平方根,然后将2 * sqrt(3)返回-将其变为(2 * 2)* 3和将sqrt(2 * 2)提取为2。

I'm using biginteger which has a very nice gcd() method and a pow() method that is restricted to positive parameters (which makes sense unless you are trying to do exactly what I'm trying to do. 我正在使用biginteger,它具有一个非常好的gcd()方法和一个pow()方法,该方法仅限于正参数(除非您尝试完全按照自己的意愿去做,否则这是有意义的。

I could come up with a few iterative ways to do this but they may take a while with numbers in the hundreds-of-digits range. 我可以想出一些迭代的方法来完成此操作,但是使用百位数范围内的数字可能要花费一些时间。

I'm hoping there is some cute, simple, non-iterative trick I haven't been exposed to. 我希望有一些我从未接触过的可爱,简单,非迭代的技巧。

Just to clarify: I have the intent to add imaginary numbers so I'm planning on results like this: 只是为了澄清一下:我有意添加虚数,所以我正在计划这样的结果:

17 + 4i √3  
-----------  
     9

Without long streams of decimals. 没有长长的小数流。

What you're asking, in essence, is to find all repeated prime factors. 本质上,您要问的是找到所有重复的主要因素。 Since you're dealing with numbers in the hundreds-of-digits range, I'm going to venture a guess here that there are no good ways to do this in general. 由于您要处理的是数百位数的数字,因此在此我大胆猜测一下,通常没有好的方法可以做到这一点。 Otherwise public key cryptography will all of a sudden be on somewhat shaky ground. 否则,公钥加密将突然处于不稳定的境地。

There are a number of methods of computing the square root . 有许多计算平方根方法 With those, you can express the result as an integer plus a remainder less than 1. 有了它们,您可以将结果表示为整数小于1的余数。

Maybe try finding the highest perfect square that is less than your number. 也许尝试找到小于您的数字的最高完美平方。 That will give you part of the equation, then you would only need to handle the remainder part which is the difference between your number and the perfect square you found. 这将为您提供一部分方程式,然后您只需要处理其余部分,这就是您的数字与找到的理想平方之间的差。 This would degrade as numbers get large as well, but perhaps not as fast. 随着数量的增加,这也会降低,但可能不会那么快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM