How can I calculate the square root without using sqrt(n) and n^0.5 ?

  • one
  • 13
    If this is a real problem, explain why the standard root does not work. If this is a training task, think for yourself, otherwise you will not learn anything. We do not have to do assignments for students. - VladD
  • By the way, there are still some violin . They are not library functions. Moreover, if you do everything correctly, you can process 4 numbers at a time. Yes, modern compilers usually use them when writing sqrt() . - KoVadim
  • one
    because such as VladD is spoiling the Russian-speaking community, the question is not simple, a person can try to understand what is written there for the function, and they immediately call him a student and pretentiously ******* tsya "We do not have to do tasks for students", already managed to divide people into "we (the elite of the Russian-speaking community, who will always be called students and send to read documentation)" and students who are looking for freebies on the stack flow - epsilon

3 answers 3

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

enter image description here :

 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; } 

ideone

  • 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."