# Calculating the square root of natural numbers

There is a certain function Where There was a need to calculate the inverse function Now I think about the elegant and not too slow method of calculating x (y). What are my options?

• I apologize, I believed the promises that the input of TeX markup is supported. @Barmaley, altered in clay tablets. - karmadro4
• I do not understand - write the formula ... - Barmaley
• @ karmadro4, This is a question for the Math forum. - Pavel S. Zaitsau
• Damn, the kaput formulas have come again. @Pavel Zaitsev, lol, no. Do not worry, this matan is not so scary :-) - karmadro4
• @ karmadro4, TeX markup is supported only on the "Mathematics" forum. - angry

Standard recurrence ratio does not fit?

``// x(n + 1) = 0.5*(x(n) + n / x(n)) scanf("%u", &test_number); x_new = test_number; x_old = 0; while (x_old != x_new) { x_old = x_new; x_new = (x_new + test_number / x_new) >> 1; } printf("%u\n", x_new);` `
• Oh, Heron's formula! I'm experimenting with her right now. By the way, it converges much faster if you feed not with zero, but with a rough estimate. - karmadro4

If I understood correctly about natural numbers, then for 32-bit integers.

` `unsigned int sqrt32(unsigned long n) { unsigned int c = 0x8000; unsigned int g = 0x8000; for(;;) { if(g*g > n) g ^= c; c >>= 1; if(c == 0) return g; g |= c; } }` `
• An interesting approach, although I understood little :-) And the restriction of natural numbers allows us not to worry about whether there is an inverse function or not. - karmadro4
• The algorithm is not mine, from the Internet. As far as I understand the software model of a piece of piece of iron. At least that's what the person who once developed iron said. - avp
• Thanks, I will try to play with him and summarize for different bit depths. - karmadro4
• Thanks, copied myself. - northerner