Find the square root of the number without using any built-in function?
up vote
0
down vote
favorite
I am working on below problem:
Given an integer, how do you find the square root of the number
without using any built-in function?
private static double computeSquareRootBinarySearch(double x, double precision) {
double start = 0;
double end = x / 2 + 1;
double mid = (start + ((end - start) / 2));
double prevMid = 0;
double diff = Math.abs(mid - prevMid);
while ((mid * mid != x) && (diff > precision)) {
if (mid * mid > x) {
end = mid;
} else {
start = mid;
}
prevMid = mid;
mid = (start + end) / 2;
diff = Math.abs(mid - prevMid);
}
return mid;
}
I came up with above binary search algo but wanted to see if there is any optimization I can do in above algorithm?
java binary-search
add a comment |
up vote
0
down vote
favorite
I am working on below problem:
Given an integer, how do you find the square root of the number
without using any built-in function?
private static double computeSquareRootBinarySearch(double x, double precision) {
double start = 0;
double end = x / 2 + 1;
double mid = (start + ((end - start) / 2));
double prevMid = 0;
double diff = Math.abs(mid - prevMid);
while ((mid * mid != x) && (diff > precision)) {
if (mid * mid > x) {
end = mid;
} else {
start = mid;
}
prevMid = mid;
mid = (start + end) / 2;
diff = Math.abs(mid - prevMid);
}
return mid;
}
I came up with above binary search algo but wanted to see if there is any optimization I can do in above algorithm?
java binary-search
If you can useMath.abs()
, then why not also useMath.sqrt()
?
– 200_success
2 days ago
Agree with @200_success, try to compute theAbs(...)
by yourself. Otherwise, why theses operations in themid
anddiff
initialization? And, it's not stated that the result must be the closest greater possible. So, why+ 1
in theend
init?
– Calak
2 days ago
a really efficent way to calculate approximations would be to use the Secant method (see en.wikipedia.org/wiki/Secant_method)
– Martin Frank
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am working on below problem:
Given an integer, how do you find the square root of the number
without using any built-in function?
private static double computeSquareRootBinarySearch(double x, double precision) {
double start = 0;
double end = x / 2 + 1;
double mid = (start + ((end - start) / 2));
double prevMid = 0;
double diff = Math.abs(mid - prevMid);
while ((mid * mid != x) && (diff > precision)) {
if (mid * mid > x) {
end = mid;
} else {
start = mid;
}
prevMid = mid;
mid = (start + end) / 2;
diff = Math.abs(mid - prevMid);
}
return mid;
}
I came up with above binary search algo but wanted to see if there is any optimization I can do in above algorithm?
java binary-search
I am working on below problem:
Given an integer, how do you find the square root of the number
without using any built-in function?
private static double computeSquareRootBinarySearch(double x, double precision) {
double start = 0;
double end = x / 2 + 1;
double mid = (start + ((end - start) / 2));
double prevMid = 0;
double diff = Math.abs(mid - prevMid);
while ((mid * mid != x) && (diff > precision)) {
if (mid * mid > x) {
end = mid;
} else {
start = mid;
}
prevMid = mid;
mid = (start + end) / 2;
diff = Math.abs(mid - prevMid);
}
return mid;
}
I came up with above binary search algo but wanted to see if there is any optimization I can do in above algorithm?
java binary-search
java binary-search
asked 2 days ago
user5447339
13817
13817
If you can useMath.abs()
, then why not also useMath.sqrt()
?
– 200_success
2 days ago
Agree with @200_success, try to compute theAbs(...)
by yourself. Otherwise, why theses operations in themid
anddiff
initialization? And, it's not stated that the result must be the closest greater possible. So, why+ 1
in theend
init?
– Calak
2 days ago
a really efficent way to calculate approximations would be to use the Secant method (see en.wikipedia.org/wiki/Secant_method)
– Martin Frank
2 days ago
add a comment |
If you can useMath.abs()
, then why not also useMath.sqrt()
?
– 200_success
2 days ago
Agree with @200_success, try to compute theAbs(...)
by yourself. Otherwise, why theses operations in themid
anddiff
initialization? And, it's not stated that the result must be the closest greater possible. So, why+ 1
in theend
init?
– Calak
2 days ago
a really efficent way to calculate approximations would be to use the Secant method (see en.wikipedia.org/wiki/Secant_method)
– Martin Frank
2 days ago
If you can use
Math.abs()
, then why not also use Math.sqrt()
?– 200_success
2 days ago
If you can use
Math.abs()
, then why not also use Math.sqrt()
?– 200_success
2 days ago
Agree with @200_success, try to compute the
Abs(...)
by yourself. Otherwise, why theses operations in the mid
and diff
initialization? And, it's not stated that the result must be the closest greater possible. So, why + 1
in the end
init?– Calak
2 days ago
Agree with @200_success, try to compute the
Abs(...)
by yourself. Otherwise, why theses operations in the mid
and diff
initialization? And, it's not stated that the result must be the closest greater possible. So, why + 1
in the end
init?– Calak
2 days ago
a really efficent way to calculate approximations would be to use the Secant method (see en.wikipedia.org/wiki/Secant_method)
– Martin Frank
2 days ago
a really efficent way to calculate approximations would be to use the Secant method (see en.wikipedia.org/wiki/Secant_method)
– Martin Frank
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
It is unlikely that
mid * mid
would ever be equal tox
, so the opportunisticmid * mid != x
consumes more cycles than it may save. I recommend to drop it entirely.The convergence rate is not the best. Your algorithm adds (approximately) one bit of precision per iteration. Compare it to a classic Newton-Raphson, which doubles the amount of correct bits per iteration.
As mentioned in the comment, without using any built-in function part of assignment rules out using
Math.abs
.You may want to check the input for correctness: both
x
andprecision
must be positive.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
It is unlikely that
mid * mid
would ever be equal tox
, so the opportunisticmid * mid != x
consumes more cycles than it may save. I recommend to drop it entirely.The convergence rate is not the best. Your algorithm adds (approximately) one bit of precision per iteration. Compare it to a classic Newton-Raphson, which doubles the amount of correct bits per iteration.
As mentioned in the comment, without using any built-in function part of assignment rules out using
Math.abs
.You may want to check the input for correctness: both
x
andprecision
must be positive.
add a comment |
up vote
2
down vote
It is unlikely that
mid * mid
would ever be equal tox
, so the opportunisticmid * mid != x
consumes more cycles than it may save. I recommend to drop it entirely.The convergence rate is not the best. Your algorithm adds (approximately) one bit of precision per iteration. Compare it to a classic Newton-Raphson, which doubles the amount of correct bits per iteration.
As mentioned in the comment, without using any built-in function part of assignment rules out using
Math.abs
.You may want to check the input for correctness: both
x
andprecision
must be positive.
add a comment |
up vote
2
down vote
up vote
2
down vote
It is unlikely that
mid * mid
would ever be equal tox
, so the opportunisticmid * mid != x
consumes more cycles than it may save. I recommend to drop it entirely.The convergence rate is not the best. Your algorithm adds (approximately) one bit of precision per iteration. Compare it to a classic Newton-Raphson, which doubles the amount of correct bits per iteration.
As mentioned in the comment, without using any built-in function part of assignment rules out using
Math.abs
.You may want to check the input for correctness: both
x
andprecision
must be positive.
It is unlikely that
mid * mid
would ever be equal tox
, so the opportunisticmid * mid != x
consumes more cycles than it may save. I recommend to drop it entirely.The convergence rate is not the best. Your algorithm adds (approximately) one bit of precision per iteration. Compare it to a classic Newton-Raphson, which doubles the amount of correct bits per iteration.
As mentioned in the comment, without using any built-in function part of assignment rules out using
Math.abs
.You may want to check the input for correctness: both
x
andprecision
must be positive.
answered 2 days ago
vnp
38.1k13096
38.1k13096
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207592%2ffind-the-square-root-of-the-number-without-using-any-built-in-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you can use
Math.abs()
, then why not also useMath.sqrt()
?– 200_success
2 days ago
Agree with @200_success, try to compute the
Abs(...)
by yourself. Otherwise, why theses operations in themid
anddiff
initialization? And, it's not stated that the result must be the closest greater possible. So, why+ 1
in theend
init?– Calak
2 days ago
a really efficent way to calculate approximations would be to use the Secant method (see en.wikipedia.org/wiki/Secant_method)
– Martin Frank
2 days ago