square-root

Infinite Recursion in Meta Integer Square Root

瘦欲@ 提交于 2019-12-05 06:54:48
Good day, A friend of mine is asking about transforming an integer square root function into a meta-function. Here is the original function: unsigned isqrt(unsigned value) { unsigned sq = 1, dlt = 3; while(sq<=value) { sq += dlt; dlt += 2; } return (dlt>>1) - 1; } I wrote a meta version using constexpr , but he said he can't use the new feature for some reason: constexpr std::size_t isqrt_impl (std::size_t sq, std::size_t dlt, std::size_t value){ return sq <= value ? isqrt_impl(sq+dlt, dlt+2, value) : (dlt >> 1) - 1; } constexpr std::size_t isqrt(std::size_t value){ return isqrt_impl(1, 3,

Generating continued fractions for square roots

与世无争的帅哥 提交于 2019-12-04 19:22:00
问题 I wrote this code for generating Continued Fraction of a square root N. But it fails when N = 139. The output should be {11,1,3,1,3,7,1,1,2,11,2,1,1,7,3,1,3,1,22} Whilst my code gives me a sequence of 394 terms... of which the first few terms are correct but when it reaches 22 it gives 12! Can somebody help me with this? vector <int> f; int B;double A; A = sqrt(N*1.0); B = floor(A); f.push_back(B); while (B != 2 * f[0])) { A = 1.0 / (A - B); B =floor(A); f.push_back(B); } f.push_back(B); 回答1:

JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

余生颓废 提交于 2019-12-04 18:12:57
I'm trying to learn algorithms and coding stuff by scratch. I wrote a function that will find square roots of square numbers only, but I need to know how to improve its performance and possibly return square roots of non square numbers function squareroot(number) { var number; for (var i = number; i >= 1; i--) { if (i * i === number) { number = i; break; } } return number; } alert(squareroot(64)) Will return 8 Most importantly I need to know how to improve this performance. I don't really care about its limited functionality yet Here is a small improvement I can suggest. First - start

Trig functions and square root in native batch?

点点圈 提交于 2019-12-04 05:37:27
问题 I am making a tool where the user is shown this triangle throughout the process: :draw echo ^|\ echo ^|a\ echo ^| \ echo ^| \ echo ^| \ C echo A^| \ echo ^| \ echo ^| \ echo ^|c b\ echo ^|---------\ echo B GOTO:EOF Where any letters are, there are variables. First the users chooses which angle value they have. Then they choose a side value. After that, all of the values will automatically be filled in. In my source code I just have sin(a) or something similar as a placeholder until I can find

Difference between **(1/2), math.sqrt and cmath.sqrt?

扶醉桌前 提交于 2019-12-04 04:02:39
What is the difference between x**(1/2) , math.sqrt() and cmath.sqrt() ? Why does cmath.sqrt() get complex roots of a quadratic right alone? Should I use that for my square roots exclusively? What do they do in the background differently? If you look at the documentation for cmath and math respectively, you will find that: cmath "provides access to mathematical functions for complex numbers" math "functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require support for complex numbers." The (**) operator maps to the pow function, with

Easiest way to find Square Root in Swift?

走远了吗. 提交于 2019-12-03 22:57:13
I have been trying to figure out how to programmatically find a square root of a number in Swift. I am looking for the simplest possible way to accomplish with as little code needed. I now this is probably fairly easy to accomplish, but can't figure out a way to do it. Any input or suggestions would be greatly appreciated. Thanks in advance Paul Buis In Swift 3, the FloatingPoint protocol appears to have a squareRoot() method. Both Float and Double conform to the FloatingPoint protocol. So: let x = 4.0 let y = x.squareRoot() is about as simple as it gets. The underlying generated code should

An efficient algorithm to calculate the integer square root (isqrt) of arbitrarily large integers

你。 提交于 2019-12-03 14:31:51
Notice For a solution in Erlang or C / C++ , go to Trial 4 below. Wikipedia Articles Integer square root The definition of "integer square root" could be found here Methods of computing square roots An algorithm that does "bit magic" could be found here [ Trial 1 : Using Library Function ] Code isqrt(N) when erlang:is_integer(N), N >= 0 -> erlang:trunc(math:sqrt(N)). Problem This implementation uses the sqrt() function from the C library, so it does not work with arbitrarily large integers (Note that the returned result does not match the input. The correct answer should be

Fast inverse square of double in C/C++

[亡魂溺海] 提交于 2019-12-03 13:36:40
问题 Recently I was profiling a program in which the hotspot is definitely this double d = somevalue(); double d2=d*d; double c = 1.0/d2 // HOT SPOT The value d2 is not used after because I only need value c. Some time ago I've read about the Carmack method of fast inverse square root, this is obviously not the case but I'm wondering if a similar algorithms can help me computing 1/x^2. I need quite accurate precision, I've checked that my program doesn't give correct results with gcc -ffast-math

Shortest way to check perfect Square? [duplicate]

拥有回忆 提交于 2019-12-03 08:22:19
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What's a good algorithm to determine if an input is a perfect square? I want Shortest and Simplest way to Check a number is perfect square in C# Some of Perfect Squares: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, ...... 回答1: Probably checking if the square root of the number has any decimal part, or if it is a whole number. Implementationwise, I would consider something like this: double result = Math.Sqrt

Fast inverse square of double in C/C++

霸气de小男生 提交于 2019-12-03 03:01:27
Recently I was profiling a program in which the hotspot is definitely this double d = somevalue(); double d2=d*d; double c = 1.0/d2 // HOT SPOT The value d2 is not used after because I only need value c. Some time ago I've read about the Carmack method of fast inverse square root, this is obviously not the case but I'm wondering if a similar algorithms can help me computing 1/x^2. I need quite accurate precision, I've checked that my program doesn't give correct results with gcc -ffast-math option. (g++-4.5) The tricks for doing fast square roots and the like get their performance by