你明白这个问题意味着什么吗?
在小于线性时间内查找整数数组中的顶部log(n)或顶部sqt(n)值.
如果你不这样做,那么问题是http://www.careercup.com/question?id=9337669.
能帮助我理解这个问题,然后可以解决它.(虽然一旦我理解我也可以解决它)
谢谢你的时间.
我在 2 年前编写的 Java 中实现了Cantor 配对函数。现在我更多地转向 iOS 我需要在 Objective-C 中做同样的事情。
问题是,至少从我的角度来看,在 Java 中我必须实现一个我自己做的BigSqrt类。因为理论上我现在可以配对任何大小的数字。
因为我是 iOS 新手,所以我真的不知道我是否必须为 Objective-C 再次实现所有东西,或者是否已经实现了一些东西。如果是这样,有人能给我一个提示,从哪里开始为Objective-C 中的任何大小的“整数”实现康托配对函数?
谢谢
我从http://blog.shay.co/newtons-method/中提取了此代码:
//a - the number to square root
//times - the number of iterations
public double Sqrt(double a, int times)
{
if (a < 0)
throw new Exception("Can not sqrt a negative number");
double x = 1;
while (times-- > 0)
x = x / 2 + a / (2 * x);
return x;
}
Run Code Online (Sandbox Code Playgroud)
对于数字的迭代次数(如果存在),有什么好的经验法则? (例如,"使用n/2次迭代".)
我正在尝试编写一个代码,它获取一个int数组并检查数字的平方根是否在数组中,如果是,则会打印是,否则它将打印否.我无法弄清楚为什么我的代码不会一直返回.
例如,如果我的输入是:1 4 0 2 16 3
我的输出将是:是是是否否是否
这是我的代码:
import java.util.Arrays;
public class Assignment02Q04 {
public static void main(String[] args) {
int[] intlist = new int[args.length];
for (int i=0; i < args.length; i++) {
intlist[i] = Integer.parseInt(args[i]);
}
for (int number : intlist) {
int sqrt = (int)(Math.sqrt(number));
System.out.println(sqrt);
if (Arrays.asList(intlist).contains(sqrt)) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的计算器代码,我想在其中包含平方根。我对 applescript 比较陌生,我不知道如何操作。可能有点菜鸟,但还是谢谢你!
我有一个c ++算法,可以计算整数的平方根.除了一个缺陷外,该程序可以正常运行.它无法计算低于1的数字的平方根.例如,它无法计算.5或.9或.0000001等的平方根,但在所有其他情况下按计划工作.我有X设置所以它不允许负输入,但我仍然不明白为什么它不会返回任何小于1的值.
include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
double squareroot(double x)
{ /* computes the square root of x */
/* make sure x is not negative .. no math crimes allowed! */
assert(x >= 0);
if (x == 0) return 0;
/* the sqrt must be between xhi and xlo */
double xhi = x;
double xlo = 0;
double guess = x / 2;
/* We stop when guess*guess-x is very small */
while (abs(guess*guess …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用包含while循环的函数来计算数字的平方根。在while循环的条件下,我想将两个值的比率的绝对值(即估计的平方根和数字)与1进行比较。但是,每当我运行程序时,我都会不断得到一个输出1.414214的无限循环。有什么帮助吗?谢谢。
// Function to calculate the absolute value of a number
#include <stdio.h>
float absoluteValue (float x)
{
if ( x < 0 )
x = -x;
return (x);
}
// Function to compute the square root of a number
float squareRoot (float x, const float epsilon)
{
float guess = 1.0;
while ( absoluteValue ((guess * guess) / x) != epsilon ) {
guess = ( (x / guess) + guess ) / 2.0;
printf("%f\n", guess);
} …Run Code Online (Sandbox Code Playgroud) 在Javascript中,我有一个方案来实现以下内容:
让我们取一个下面的整数列表,取该列表中的每个整数并将该值加倍,然后将doubled值平方,然后作为输出,将所有平方值相加.例如:
doubleandSquareandSum[1] // 4
doubleandSquareandSum[1, 2] // 20
doubleandSquareandSum[1, 2, 3] // 56
doubleandSquareandSum[1, 2, 3, 4] // 120
Run Code Online (Sandbox Code Playgroud)
JS Code尝试过:
function doubleandSquareandSum(arr) {
ret= [];
for (var i = 0, len = arr.length; i < len; i++) {
ret.push(arr[i] * arr[i]);
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
我知道Math.sqrt,但不知道如何在这里实现它.我尝试了以上的东西,但没有奏效.有人可以帮我吗?
因此,我需要在Java 9之前的版本中使用BigInteger,我发现下面的函数可以做到这一点。我确实了解该代码,但是我真的不明白为什么要在那里。因此,我想我并没有真正了解其背后的数学原理。就像为什么要使用(n / 32 + 8)。为什么要计算中间值。等等
BigInteger a = BigInteger.ONE;
BigInteger b = n.shiftRight(5).add(BigInteger.valueOf(8));
while (b.compareTo(a) >= 0) {
BigInteger mid = a.add(b).shiftRight(1);
if (mid.multiply(mid).compareTo(n) > 0) {
b = mid.subtract(BigInteger.ONE);
} else {
a = mid.add(BigInteger.ONE);
}
}
return a.subtract(BigInteger.ONE);
}
Run Code Online (Sandbox Code Playgroud) 并不是我不明白如何求一个数的整数平方根。我知道使用 Python 和 C++ 查找它们的几种方法。
\n只是这个算法实在是太伤脑筋了。而且必须用 SML 编写它是另一个令人头疼的问题。
\n请帮助我理解这个算法。请注意,这应该使用递归:
\n\n\n的整数平方根是满足 \xc2\xb2\xe2\x89\xa4<(+1)\xc2\xb2 的整数。\n可以使用以下归纳过程计算整数平方根:
\n\n
\n- 递归计算 = div 4 的整数平方根。然后我们有 \xc2\xb2\xe2\x89\xa4<(+1)\xc2\xb2。
\n- 由于 和 是整数,所以我们有 (+1)\xe2\x89\xa4(+1)\xc2\xb2。因此我们有 (2)\xc2\xb2\xe2\x89\xa44\xe2\x89\xa4<4+4\xe2\x89\xa4(2+2)\xc2\xb2。
\n- 因此我们知道 的整数平方根是 2 或 2+1。
\n编写与上述算法相对应的递归 ML 程序。
\n
square-root ×10
algorithm ×2
arrays ×2
c ×2
c++ ×2
java ×2
applescript ×1
biginteger ×1
c# ×1
calculator ×1
function ×1
ios ×1
javascript ×1
list ×1
mapping ×1
math ×1
recursion ×1
sml ×1
smlnj ×1
while-loop ×1