- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在编写一个函数来计算给定 BigInteger 的平方根。我的测试文件中的当前数字是 250074134890485729738。然而,程序总是在找到 15813732488 处的 sqrt 时停止,其平方是 250074135202026670144。我已经复制了这个来自另一个 StackOverflow 问题的代码,并且它停止收敛于相同的数字。它使用牛顿法,而我使用的是巴比伦法/海伦法。
他们的代码:
public static BigInteger sqrtN(BigInteger in) {
final BigInteger TWO = BigInteger.valueOf(2);
int c;
// Significantly speed-up algorithm by proper select of initial approximation
// As square root has 2 times less digits as original value
// we can start with 2^(length of N1 / 2)
BigInteger n0 = TWO.pow(in.bitLength() / 2);
// Value of approximate value on previous step
BigInteger np = in;
do {
// next approximation step: n0 = (n0 + in/n0) / 2
n0 = n0.add(in.divide(n0)).divide(TWO);
// compare current approximation with previous step
c = np.compareTo(n0);
// save value as previous approximation
np = n0;
// finish when previous step is equal to current
} while (c != 0);
return n0;}
我的代码:
static BigInteger number;
static BigInteger sqrt;
public static void main(String[] args) throws Exception {
number = new BigInteger(getFile());
System.out.println("Factoring: \n\n" + number);
sqrt = sqrt();
System.out.println("The root is: " + sqrt.toString());
System.out.println("Test, should equal nearest square at or above original number: " + sqrt.multiply(sqrt).toString() + "\nOriginal number: " + number.toString());
}
public static BigInteger sqrt() {
BigInteger guess = number.divide(new BigInteger("500"));
BigInteger TWO = new BigInteger("2");
BigInteger HUNDRED = new BigInteger("100");
boolean go = true;
while (number.subtract((guess.multiply(guess))).abs().compareTo(HUNDRED) == 1 && go){
BigInteger numOne = guess.divide(TWO);
BigInteger numTwo = number.divide(guess.multiply(TWO));
guess = numOne.add(numTwo);
if (numOne.equals(numTwo))
go = false;
System.out.println(guess.toString());
}
return guess.add(BigInteger.ONE);
我的输出:
保理:
250074134890485729738
250074134890485979
125037067445243488
62518533722622743
31259266861313370
15629633430660684
7814816715338341
3907408357685169
1953704178874583
976852089501290
488426044878644
244213022695321
122106511859659
61053256953828
30526630524913
15263319358455
7631667871224
3815850319588
1907957927606
954044498302
477153309146
238838702566
119942872245
61013907968
32556274730
20118781556
16274333124
15820250501
15813733820
15813732478
15813732478
The root is: 15813732479
Test, should equal nearest square at or above original number: 250074134917379485441
Original number: 250074134890485729738
一些注意事项:
回答:这实际上是按原样工作的,原始输入根本不是一个完美的正方形。因此,这非常适合我的目的。感谢所有因我的无能而浪费时间的人。我更改了代码以返回一个等效于(如果 Math.sqrt/ceil 在 BigInts 上工作)的值:
sqrt = Math.Ceil(Math.Sqrt(A_RANDOM_BIGINTEGER_HERE));
我还删除了不必要的变量,并更新了输出以匹配。这两种方法都可以正常工作,尽管第一种方法需要一些代码来捕获非收敛循环,以防将来此问题的任何访问者希望使用它们。
最佳答案
15813732478 是250074134890485729738 的平方根,至少是它的整数部分。根据 calc
,真正的平方根是 15813732478.149670840219509075711。
有两个问题:
sqrt(N)*sqrt(N) = N
的假设是错误的,因为您只计算积分部分,所以会出现与 N< 成正比的误差
。关于java - BigInteger Sqrt 函数不收敛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20985077/
我有几个关于我的遗传算法和整体 GA 的问题。 我创建了一个 GA,当给定一条曲线时,它会尝试找出产生这条曲线的函数。 例子如下积分 {{-2, 4},{-1, 1},{0, 0},{1, 1},{2
我正在尝试编写一个 GA 来解决以下难题... 二进制编码(我认为)非常有效。每件作品可以是: 原始向上或翻转的方式 - 1 位 旋转 0(即无)、90、180 或 270 度 - 2 位 在位置 (
我正在编写一个小代码(顺序)来计算适度数据集的网页排名(尽管并非完全微不足道)。 算法是这样的: while ( not converged ) { // Do a bunch of thing
我正在尝试检测长时间序列中的微事件。为此,我将训练一个 LSTM 网络。 数据。每个时间样本的输入是 11 个不同的特征,经过一定程度的标准化以适合 0-1。输出将是两个类之一。 批处理。由于巨大类别
我试图通过使用 optim 函数在 R 中找到最佳 GARCH 模型的参数。但是,我的值(value)观会变得很高,这是没有意义的。我在 MATLAB 中使用 fminsearch 实现了类似的算法,
我运行了 20 倍 cv.glmnet 套索模型以获得 lambda 的“最佳”值。但是,当我尝试重现 glmnet() 的结果时,我收到一条错误消息: Warning messages: 1: fr
我在 dymola 中构建了一个模型。虽然在初始化过程中出现了一些错误,但最终还是计算成功了。 模型收敛成功后,我尝试使用“在模型中保存起始值”选项将正确的迭代变量 strat 值存储到模型中,以便模
我有一个分层 Logit,可以随着时间的推移进行观察。正在关注Carter 2010 ,我添加了时间、时间^2 和时间^3 术语。在添加时间变量之前,模型会使用 Metropolis 或 NUTS 进
再次感谢您花时间阅读这篇文章。 我知道这个问题已经被问了很多,而且我已经检查了很多关于这个问题的帖子:然而,我对使用反向传播的成功 XOR 学习的探索仍未完成。 我按照建议尝试调整学习率、动量、有/无
我是一名优秀的程序员,十分优秀!