简体   繁体   English

Integer 破解密码书平方根算法中的范围

[英]Integer range in square root algorithm of Cracking the Code book

There is an algorithm in java for square root in cracking the code book as below: java 中有一个用于破解密码本的平方根算法如下:

int sqrt(int n) {
  return sqrt_helper(n, 1, n);
}

int sqrt_helper(int n, int min, int max) {
  if (max < min) return -1; 
  int guess = (min + max) / 2·,
  if (guess *guess == n) { 
    return guess;
  } else if (guess * guess < n) { 
    return sqrt_helper(n, guess + 1, max); 
  } else {  
    return sqrt_helper(n, min, guess - l); 
  }
}

The question is:问题是:

As min and max are integer, they can have any values in the range, ie max = Integer.MAX_VALUE由于minmax是 integer,它们可以有范围内的任何值,即max = Integer.MAX_VALUE

So how not be worry about guess = (min + max) / 2 as it will cross the allowed range, or guess *guess also.那么如何不用担心guess = (min + max) / 2因为它会越过允许的范围,或者guess *guess也是。

Since you mention "Cracking the Coding Interview"...既然你提到了“破解编码面试”......

Typically in the context of the average coding interview one wouldn't worry about implementation-specific details like this.通常在普通编码面试的背景下,人们不会担心这样的特定于实现的细节。 The interviewer is trying to confirm basic competency and understanding - they'll rarely want to run your code, and it should be a given for both of you that the algorithm will break down at the extreme limits of your language's basic data types.面试官试图确认基本的能力和理解——他们很少想要运行你的代码,并且应该给你们两个都知道算法会在你的语言基本数据类型的极端限制下崩溃。 If the interviewer asks specifically about limitations, then you could briefly mention that the function will fail for values higher than (Integer.MAX_VALUE / 2) in this language.如果面试官特别询问限制,那么您可以简单地提到 function 在该语言中对于高于 (Integer.MAX_VALUE / 2) 的值将失败。

The limitation will apply to almost any algorithm you write for a coding interview, and no reasonable interviewer would expect you to specifically design your solution to mitigate this kind of edge case.该限制将适用于您为编码面试编写的几乎所有算法,并且没有合理的面试官会期望您专门设计您的解决方案来缓解这种边缘情况。 I would find it extremely off-putting if I asked a candidate to write a function that produces Fibonacci numbers and they spent time trying to optimize the case where the output exceeds 16 digit values.如果我让应聘者编写一个产生斐波那契数的 function 并且他们花时间尝试优化 output 超过 16 位值的情况,我会觉得这非常令人反感。

If for some reason you needed to find the square root of extremely large values using this algorithm in a real life scenario, I'd expect you'd to have to implement it using a generic big number library for your particular language.如果出于某种原因您需要在现实生活场景中使用此算法找到极大值的平方根,我希望您必须使用针对您的特定语言的通用大数库来实现它。 That being said, I wouldn't roll my own square root algorithm for any real use case under almost any circumstance.话虽如此,在几乎任何情况下,我都不会为任何实际用例推出自己的平方根算法。

There are simple ways of getting around that problem (like min + (max - min) / 2 ).有一些简单的方法可以解决这个问题(比如min + (max - min) / 2 )。

The more serious integer overflow problem is guess * guess .更严重的 integer 溢出问题是guess * guess You could change the test to compare guess with n / guess , which is slower but normally won't overflow.您可以更改测试以将guessn / guess进行比较,后者较慢但通常不会溢出。 Or you could use a bit hack to find a better starting point ( clz is useful here, if you have it), since you should be able to find a guess whose square is within the range of representable integers.或者您可以使用一些技巧来找到更好的起点( clz在这里很有用,如果有的话),因为您应该能够找到平方在可表示整数范围内的猜测。

An interviewer might be even more impressed if you were able to provide the Newton-Raphson algorithm , which converges extremely rapidly.如果您能够提供收敛速度极快的Newton-Raphson 算法,面试官可能会更加印象深刻。

You'd have to confirm your language.您必须确认您的语言。 But in pseudo-code you can do something like:但在伪代码中,您可以执行以下操作:

int guess = ((min.ToBigInt() + max.ToBigInt()) / 2).ToInt()

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

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