-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.
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.020There's also http://codegolf.stackexchange.com/questions/30253/calculate-the-square-root-only-using
– a spaghetto – 2016-02-08T22:34:29.603Is 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
– FryAmTheEggman – 2016-02-08T23:15:13.797500000000000000000
as input, the output should be707106781.18654752
ish, is the result then707106000
? Can it be in scientific notation? These general floating point issues make a good case for using the sandbox.1With this definition, even division would be disallowed, as
x/y
is equal toxy^(-1)
. – LegionMammal978 – 2016-02-08T23:53:45.1532I 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