Find the square root, BUT

-2

Write a square root function that takes an input, positive or negative, and calculate it's square root, correct up to six decimal places (then truncate after the sixth decimal place). But, you cannot use ANY built in power related functions. This means nothing like (but not limited to):

Math.pow(n,0.5)
sqrt(2)
root(n,2)
n**0.5
etc. 

This means that you're going to have to use (or create) a method to find the square root. Two common methods are the Babylonian/bisector method and Newton's method, but feel free to create your own!

Output can be either printed to the console or returned. Your choice.

Examples:

Input:
sqrt(4) //you can call your function whatever you want
Output:
2
Input:
7
Output:
2.645751
Input:
5.0625
Output:
2.25
Input:
-15
Output:
3.872983i
Input:
-9
Output:
3i

This is code golf, so shortest code in bytes wins!

Remember, no built ins that have anything to do with powers!

If you have any questions, please, tell me in the comments and I'll clarify promptly.

Daniel

Posted 2016-02-08T22:16:10.193

Reputation: 6 425

Question was closed 2016-02-08T23:56:28.880

1Is e^x allowed? (I'm guessing not) – a spaghetto – 2016-02-08T22:29:02.090

While this challenge doesn't ban power functions, there are enough answers there that don't use them to call this one a dupe, IMO.

– Geobits – 2016-02-08T22:33:32.020

Is an xth root function power-related? – lirtosiast – 2016-02-08T22:37:26.690

1Also, 6 decimal places is somewhat ambiguous, do you mean six significant figures? Otherwise this becomes dramatically more complicated given a 50 digit input. – FryAmTheEggman – 2016-02-08T22:42:15.237

1

"Do X without Y" challenges are generally a bad idea, and this one hits the usual pitfalls.

– xnor – 2016-02-08T23:01:15.107

@quartata - no, that is not allowed as that is power (^) – Daniel – 2016-02-08T23:04:24.037

@ThomasKwa - wouldn't xth root be equivalent to n^(1/x), which is power – Daniel – 2016-02-08T23:05:49.097

@FryAmTheEggman - I'm not sure exactly what you mean. How is six decimal places ambiguous? If you have 1.2345678901, then you would truncate to 1.234567 – Daniel – 2016-02-08T23:10:49.680

2

Try, for example 500000000000000000 as input, the output should be 707106781.18654752 ish, is the result then 707106000? Can it be in scientific notation? These general floating point issues make a good case for using the sandbox.

– FryAmTheEggman – 2016-02-08T23:15:13.797

1With this definition, even division would be disallowed, as x/y is equal to xy^(-1). – LegionMammal978 – 2016-02-08T23:53:45.153

2I voted to close this as unclear and the other one as a dupe. The old challenge was broken (all of the answers were just **.5), whereas this has a better chance of being a good challenge. I am concerned, like @xnor, about the viability of a challenge of this type, however. – lirtosiast – 2016-02-08T23:58:22.043

@FryAmTheEggman - the output cannot be in scientific notation. Your example would have an output of 707106781.186547 – Daniel – 2016-02-09T00:04:31.370

1@LegionMammal978 - I don't mean to be so restrictive. I just want to find an objective way to ensure that nobody uses trivial answers that let them not write or not implement an algorithm to find the square root. This challenge is essentially to write the shortest code to find the square root of a number like the system would, in that a system cannot use built ins because it is the built in. Does that make more sense? – Daniel – 2016-02-09T00:09:39.753

@Dopapp I think this is an interesting idea for a challenge, although there are some parts to it that are unclear. Would you mind if I were to post the same challenge but with clearer rules? (I won't do it if you'd like to do it yourself) – James – 2016-06-20T16:42:44.137

Go for it @DrGreenEggsandIronMan! I'll be looking out for your version :) – Daniel – 2016-06-20T17:00:47.170

Answers

3

c (function), 166

Now with complex numbers. Uses the GCC complex number extension.

Straightforward golfing of the famous Fast inverse square root method. This method gives 1/sqrt(n), so we simply multiply this by n to give sqrt(n):

#include <complex.h>
#define M y*=1.5-n*y*y/2
i;_Complex float f(float m){float y,n;n=m>0?m:-m;y=n;i=0x5f3759df-*(int*)&y/2;y=*(float*)&i;M;M;M;return m>0?y*n:y*n*I;}

The famous snippet of Quake code only does one iteration of Newton's method, which gives a fairly inaccurate answer. Three iterations here seems to be enough for 6 decimal places, at least for the testcases.

Try it online.

Digital Trauma

Posted 2016-02-08T22:16:10.193

Reputation: 64 644

Isn't this not-your-IP and should thus be CW? Or could you at least link to the source file (there are github repos of Quake source)? – cat – 2016-04-14T00:24:07.043

@cat OK - CWed and github URL added. – Digital Trauma – 2016-04-14T05:26:15.047