在另一个不相关的互联网论坛上,有人询问如何检查给定数字的平方根是否为整数.现在本身就是一个微不足道的家庭作业问题,但我开始怀疑这种天真的方法在所有情况下是否都是正确的.也就是说,在伪代码中:
declare x, y as double
input x
y = sqrt(x)
if round(y) = y then
output "Is integer"
else
output "Isn't integer"
Run Code Online (Sandbox Code Playgroud)
是否有可能输入这样一个x,它x本身不是一个整数(或一个不是另一个整数的平方的整数),但由于浮点错误sqrt(x) 会是整数?
我在互联网上搜索了一个函数来使用 Scala 编程语言找到 BigInt 的精确平方根。我没有得到,但看到了一个 Java 程序,我将该函数转换为 Scala 版本。它正在工作,但我不确定它是否可以处理非常大的 BigInt。但它只返回 BigInt。不是 BigDecimal 作为平方根。它表明在代码中进行了一些位操作,并使用了一些数字的硬编码,例如shiftRight(5), BigInt("8") and shiftRight(1). 我可以清楚地理解逻辑,但不是这些位移数字和数字 8 的硬编码。可能是这些位移功能在 Scala 中不可用,这就是为什么需要在少数地方转换为 java BigInteger 的原因。这些硬编码的数字可能会影响结果的精度。我只是将 java 代码更改为 Scala 代码,只是复制了确切的算法。这是我在 Scala 中编写的代码:
def sqt(n:BigInt):BigInt = {
var a = BigInt(1)
var b = (n>>5)+BigInt(8)
while((b-a) >= 0) {
var mid:BigInt = (a+b)>>1
if(mid*mid-n> 0) b = mid-1
else a = mid+1
}
a-1
}
Run Code Online (Sandbox Code Playgroud)
我的要点是:
shiftRight(5), shiftRight(1) and 8与结果的精度相关。我在 Scala REPL 中测试了一个数字:该函数 …
我有一个问题,我的二进制搜索算法找到2的平方根似乎是在一个无限循环并永远运行:
num = 2
low = 1
high = num
i = 0
while((i**2) != 2): #was while(low<high): but wasnt working with it either
i = low + (high - low) / 2;
sqrt = i * i
if (sqrt == num):
print(i)
elif(sqrt < num):
low = i
else:
high = i
print(sqrt)
testRoot = 2 ** (.5)
print(testRoot)
Run Code Online (Sandbox Code Playgroud)
我不确定我的while循环是否存在问题.我认为这将是一个非常直接的二进制搜索算法,稍作修改以适应平方根方面.
在运行我的代码时,我似乎无法让它产生任何输出.我不确定代码或编译器是否存在真正的问题,因为我认为我的算法与我过去的算法非常接近.
我尝试使用二分搜索来查找整数的平方根,但有些我无法通过一些测试用例。
我能够传递 mySqrt(4) = 2,但无法传递 mySqrt(2147395599)
关于我搞砸的地方有什么想法吗?
public static int mySqrt(int x) {
int left = 0;
int right = x;
if(x < 2){
return x;
}
while(left < right){
int mid = left + ((right - left) / 2);
if(mid * mid == x){
return mid;
}
else if(mid * mid < x){
left = mid + 1;
}
else{
right = mid;
}
}
return left - 1;
}
Run Code Online (Sandbox Code Playgroud) 我试图得到一个(9)数字的数组,然后打印,但我一直只带回一个结果 - 数组中的数字平方 - 显然不是我想要的.谢谢你的帮助.好的,到目前为止,这是我可怕的代码.试图将它传递给方法.
public static void main ( String args[] )
{
double[] nums = {126, 12.939, 795, 320.16,
110, 34.7676, 7773, 67, 567, 323};
System.out.println ("Square root is " +square);
square(nums);
}
public static double square (double [] array) {
double result;
for( double i = 0; i < array.length ; i++ )
result = Math.sqrt(array[i]);
return result;
}
}
Run Code Online (Sandbox Code Playgroud) 我想计算双精度矢量的平方和平方根.例如给出:
vector<double> Array1(10,2.0);
vector<double> Array2(10,2.0);
for(unsigned int i=0; i<Array1.size(); i++)
Array1[i] = sqrt(Array1[i]);
for(unsigned int i=0; i<Array2.size(); i++)
Array2[i] = Array2[i] * Array2[i];
Run Code Online (Sandbox Code Playgroud)
有没有办法使用STL函数,如变换?也许有一个内置的sqrt函数作用于数组?
我正在尝试调用该dist()方法但是我一直收到错误,说dist()必须返回一个值.
// creating array of cities
double x[] = {21.0,12.0,15.0,3.0,7.0,30.0};
double y[] = {17.0,10.0,4.0,2.0,3.0,1.0};
// distance function - C = sqrt of A squared + B squared
double dist(int c1, int c2) {
z = sqrt ((x[c1] - x[c2] * x[c1] - x[c2]) + (y[c1] - y[c2] * y[c1] - y[c2]));
cout << "The result is " << z;
}
void main()
{
int a[] = {1, 2, 3, 4, 5, 6};
execute(a, 0, sizeof(a)/sizeof(int));
int …Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题,我的X方格总是变得无穷大,导致产生的距离也是无限的,但是我看不出自己的数学有什么问题:
// Claculate distance
xSqr = (x1 - x2) * (x1 - x2);
ySqr = (y1 - y2) * (y1 - y2);
zSqr = (z1 - z2) * (z1 - z2);
double mySqr = xSqr + ySqr + zSqr;
double myDistance = sqrt(mySqr);
Run Code Online (Sandbox Code Playgroud)
当我运行我的程序时,我得到每个坐标的用户输入,然后在我运行计算后显示距离.
我正在制作一个工具,用户在整个过程中都会看到这个三角形:
:draw
echo ^|\
echo ^|a\
echo ^| \
echo ^| \
echo ^| \ C
echo A^| \
echo ^| \
echo ^| \
echo ^|c b\
echo ^|---------\
echo B
GOTO:EOF
Run Code Online (Sandbox Code Playgroud)
任何字母都有,有变数.首先,用户选择他们具有的角度值.然后他们选择一个边值.在那之后,所有的值都将被自动填充.在我的源代码中,我只有sin(a)或者类似于占位符的东西,直到我可以在本机批处理中找到trig函数(sin,cos,tan)和squareroot.
众所周知,如果自然数的平方根不是整数,那么它是不合理的.这种平方根的十进制扩展是无限的,没有任何重复模式.
二的平方根是
1.41421356237309504880...,并且前一百个十进制数字的数字和是475.对于前100个自然数,找到所有无理平方根的前100个十进制数字的数字和的总和.
这是我为这个问题制作的代码:
from decimal import *
from math import sqrt
getcontext().prec = 100
def digitalsum(n):
sum = 0
for a in n:
sum += int(a)
return sum
total = 0
for a in range(1, 101):
if not sqrt(a) % 1 == 0:
ans = str(Decimal(a).sqrt())
ans = ans[2::]
print(a)
print(digitalsum(ans))
print("-------")
total += digitalsum(ans)
print(total)
Run Code Online (Sandbox Code Playgroud)
它显示错误的答案,我认为我一路上都错过了一些东西.任何形式的帮助表示赞赏.
square-root ×10
c++ ×3
distance ×2
java ×2
python ×2
3d ×1
arrays ×1
batch-file ×1
exponent ×1
math ×1
math.h ×1
scala ×1
stl ×1
trigonometry ×1
vector ×1