How can I calculate the square root without using sqrt(n)
and n^0.5
?
3 answers
The question actually has many solutions.
The most commonplace is the half division method.
double l = 0; double r = 1e100; //большое число double m; while (r - l > 1e-8){ //точность m = l + (r - l)/2; if (m*m > n) l = m; else r = m; } //ответ в l
There are more original ways, for example, simulation of a calculation in a column (here's an example , I will not give the code)
The method is more for C, but I think you can use it in Java. Explanation
float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; i = 0x5f3759df - ( i >> 1 ); y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1 итерация // y = y * ( threehalfs - ( x2 * y * y ) ); // 2 итерация, можно удалить return 1/y; }
You can use logarithms
return Math.exp( Math.log(n) / 2);
You can use numerical methods, such as Newton's method
double x = 1; for (;;) { double nx = (x + n / x) / 2; if (abs (x - nx) < 1e-10) break; //точность x = nx; }
There are many other ways, it all depends on specific requirements.
- 2
for(int i=2; i <= 9;i++) {
if(number%i == 0) {
if(number/i == i) {
root = true;
System.out.println("Корень - " + i);
}
}
}
I did it like this. fit? - Tsyklop - @Tsyklop khm ... for exact squares within 81 is like. - pavel
Write your own square root function using the Newton method (tangents) using the formula
public static double sqrt(int number) { double t; double squareRoot = number / 2; do { t = squareRoot; squareRoot = (t + (number / t)) / 2; } while ((t - squareRoot) != 0); return squareRoot; }
- And by what principle does everything happen here? I now understand the blunt - Aleksey Shimansky
- 2@ Alexey Shimansky method of tangents, he is Newton.
number / 2
is the initial approximation. - pavel - @pavel I mean, the explanation would be nice ..... but do not
Напишите свою собственную функцию вычисления
...... as if he himself invented ...... spanked the code, and then whatever you want, then do - Alexey Shimansky - one@ Alexey Shimansky Run in the debager and see. everything becomes understandable. It became personally clear to me. Thanks to the author. - Tsyklop
- one@Tsyklop, my message was still not that I was not a boom boom at all, but that the author did not bother to explain what it was and why, so that others could decide who would find out the answer to this question - see and learn, not just copy pastes .... what, in principle, makes this comrade - Aleksey Shimansky
It all depends on the context of the task. About the various methods have already been told. There is another option for the case if the range of input parameters is approximately known. You can simply make a table with ready-made answers.
When they did the coursework on microcircuitry for 8-bit systems, the simplest and fastest solution was a 256-cell ROM with the answers already wired there. But the teacher did not agree with this approach. Proposed all the same "count."
sqrt()
. - KoVadim