我正在制作一个C ++程序来计算数字的平方根。该程序不使用内置操作的“sqrt”数学运算。有两个变量,一个用于用户输入的数字,另一个用于该数字的平方根。这个程序效果不好,我相信还有更好的方法:
以下是我的完整代码:
#include <iostream>
using namespace std;
int main(){
int squareroot = 0;
int number;
cout << "enter a number sp that i can calculate its squareroot" << endl;
cin >> number;
while (squareroot * squareroot != number){
squareroot+=0.1;
}
cout << "the square root is" << squareroot << endl;
return 0;
}
我知道必须有更好的方法。请帮忙。通过谷歌看,但不理解那里的复杂程序,因为我还是初学者。
提前致谢。
答案 0 :(得分:2)
下面给出了整数平方根计算的解释:
在数论中,正整数的平方根 整数n是正整数m,它是最小的整数 大于或等于n的平方根
你开始的方法很好,但需要多次修正以使其有效:
您正在使用int
想要将{1}添加到squareroot
而不是0.1
您希望在squareroot * squareroot
等于或大于number
时停止计算。想想这个案例的数字是26,你没有一个整数乘以26。
在数字等于26的情况下,你想要返回5还是6?在while
循环后,squareroot
的值将为6,因此您可能希望将其反转为5(如果squareroot * squareroot
与number
不同)
下面的例子:
#include <iostream>
using namespace std;
int main(){
int squareroot = 0;
int number;
cout << "enter a number sp that i can calculate its squareroot" << endl;
cin >> number;
while (squareroot * squareroot < number){
squareroot+=1;
}
if (squareroot * squareroot != number) --squareroot;
cout << "the square root is" << squareroot << endl;
return 0;
}
下面是一种使用二分搜索原理计算平方根的更有效和更优雅的方法。为O(log(n))的
int mySqrt(int x) {
if (x==0) return 0;
int left = 1;
int right = x/2 + 1;
int res;
while (left <= right) {
int mid = left + ((right-left)/2);
if (mid<=x/mid){
left = mid+1;
res=mid;
}
else {
right=mid-1;
}
}
return res;
}
答案 1 :(得分:1)
如果A不是一个完美的正方形,这个函数将计算平方根的底面。这个函数基本上使用二进制搜索。你事先知道的两件事是数字的平方根将小于或等于那个数字,它将会大于或等于1.所以我们可以在那个范围内应用二进制搜索。下面是我的实现。如果你对代码中没有任何理解,请告诉我。希望这有帮助。
int sqrt(int A) {
if(A<1)return 0;
if(A==1)return 1;
unsigned long long start,end,mid,i,val,lval;
start = 1;
end = A;
while(start<=end){
mid = start+(end-start)/2;
val = mid*mid;
lval = (mid-1)*(mid-1);
if(val == A)return mid;
else if(A>lval && A<val) return mid-1;
else if(val > A)end = mid;
else if(val < A)start = mid+1;
}
}
答案 2 :(得分:1)
此功能使用嵌套间隔(未经测试),您可以定义准确度:
#include <math.h>
#include <stdio.h>
double mySqrt(double r) {
double l=0, m;
do {
m = (l+r)/2;
if (m*m<2) {
l = m;
} else {
r = m;
}
}
while(fabs(m*m-2) > 1e-10);
return m;
}
答案 3 :(得分:1)
你的代码的问题在于它只有在数字的平方根正好是N * 0.1时才有效,其中N是一个整数,这意味着如果答案是1.4142而不是1.400000000,那么你的代码将会失败。有更好的方法,但它们都更复杂,并使用数值分析来近似答案,最简单的是Newton-Raphson方法。
您可以使用下面的函数,此函数使用Newton-Raphson方法查找根,如果您需要有关Newton-Raphson方法的更多信息,请查看this维基百科文章。如果你需要更高的准确度 - 但是性能更差 - 你可以减少'0.001'你的比较,或者如果你想要更好的性能但是更低的准确度会增加它。
float mysqrt(float num) {
float x = 1;
while(abs(x*x - num) >= 0.001 )
x = ((num/x) + x) / 2;
return x;
}
如果您不想导入math.h
,可以自行编写abs()
:
float abs(float f) {
if(f < 0)
f = -1*f;
return f;
}
答案 4 :(得分:1)
数字的平方根,因为数字是一个完美的平方。
复杂度为log(n)
/**
* Calculate square root if the given number is a perfect square.
*
* Approach: Sum of n odd numbers is equals to the square root of n*n, given
* that n is a perfect square.
*
* @param number
* @return squareRoot
*/
public static int calculateSquareRoot(int number) {
int sum=1;
int count =1;
int squareRoot=1;
while(sum<number) {
count+=2;
sum+=count;
squareRoot++;
}
return squareRoot;
}
答案 5 :(得分:0)
#include <iostream>
using namespace std;
int main()
{
double x = 1, average, s, r;
cout << "Squareroot a Number: ";
cin >> s;
r = s * 2;
for ( ; ; ) //for ; ; ; is to run the code forever until it breaks
{
average = (x + s / x) / 2;
if (x == average)
{
cout << "Answer is : " << average << endl;
return 0;
}
x = average;
}
}
您可以尝试我的代码:D 我在这里使用的方法是巴比伦平方根方法 您可以在这里https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
中找到它