C
Obviously this is the best way. It's as fast as you can imagine by looking at the code. Using C, because C is fastest, and this problem requires a fast solution. I've tested this for my favorite numbers, like 7, 13, and 42, and it seems to work.
double square_root(int number) {
const double results[] = {
0.0000000, 1.0000000, 1.4142136, 1.7320508, 2.0000000,
2.2360680, 2.4494897, 2.6457513, 2.8284271, 3.0000000,
3.1622777, 3.3166248, 3.4641016, 3.6077713, 3.7426574,
3.8729833, 4.0000000, 4.1231056, 4.2426407, 4.3588989,
4.4721360, 4.5825757, 4.6904158, 4.7958315, 4.8989795,
5.0000000, 5.0990195, 5.1961524, 5.2915026, 5.3851648,
5.4772256, 5.5677644, 5.6568542, 5.7445626, 5.8309519,
5.9160798, 6.0000000, 6.0827625, 6.1644140, 6.2449980,
6.3245553, 6.4031242, 6.4807407, 6.5574342, 6.6332496,
6.7082039, 6.7823300, 6.8556546, 6.9282032, 7.0000000,
7.0710678, 7.1414284, 7.2111026, 7.2801099, 7.3484692,
7.4161985, 7.4833148, 7.5498344, 7.6157731, 7.6811457,
7.7451337, 7.8102497, 7.8740079, 7.9372539, 8.0000000,
8.0622577, 8.1420384, 8.1853528, 8.2462113, 8.3066239,
8.3666003, 8.4261498, 8.4852814, 8.5440037, 8.6023253,
8.6602540, 8.7177979, 8.7749644, 8.8317609, 8.8881942,
8.9442719, 9.0000000, 9.0553851, 9.1104336, 9.1651514,
9.2195425, 9.2736185, 9.3273791, 9.3808315, 9.4339811,
9.4861337, 9.5393920, 9.5914230, 9.6436508, 9.6953597,
9.7467943, 9.7979590, 9.8488578, 9.8994949, 9.9498744,
};
return number[results];
}
63@CloseVoters Please stop voting to close [code-trolling] as off topic because of lack of winning criteria. In this case, this is obviously a [popularity-contest]. Edit it in if you want to. Just because you don't like [code-trolling] doesn't mean that you have to close the challenge for everyone else. Obviously, many people like this type of challenge, evidenced by the number of answers, so, since SE is a community-driven site, leave it open for these people. – Justin – 2014-04-21T08:01:46.080
12@Quincunx For the record, my vote was for Too Broad. There's literally nothing to go by except "make something related to square roots." (As evidenced by there being fifteen answers already.) – Doorknob – 2014-04-21T11:57:25.673
1Oh great, there's been two more answers in 15 minutes, and this is in the hot network questions already. This is why code trolling is so incredibly meh – Doorknob – 2014-04-21T12:14:54.870
7Close-voters: Can you help me understand how this is any more "broad" than other unclosed [code-trolling] questions? Maybe there's a valid close reason here, but the code-trolling category is naturally going to be a bit broader than most challenges. Otherwise, it would somewhat defeat the purpose. – Geobits – 2014-04-21T13:18:31.433
In addition to being too broad, it is a possible duplicate of http://codegolf.stackexchange.com/q/73/18487 with the winning criterion changed. Not enough in my opinion to merit a brand new challenge.
– Rainbolt – 2014-04-21T13:36:07.2376@Geobits, by saying that the answer doesn't even have to be correct, this is as broad as "Write some code". – Peter Taylor – 2014-04-21T13:54:53.967
@Quincunx Many people like McDonald's food. I'd still tell everyone that their food is crap and they should eat elsewhere. – Gareth – 2014-04-21T15:17:07.397
11@Gareth It's an interesting analogy, but it doesn't match the original comment. You're most welcome to stand outside McDonalds all day with a banner telling people the food is crap. And you are most welcome to downvote/put negative comments on this question. However if you try to physically stop people from entering McDonalds (the equivalent of the second round of close voting, which has just begun) you'll most likely get arrested. I've some sympathy with the anti code-trolling cause and I will not be upvoting (or downvoting) this question. But I want freedom to post an answer if I have one. – Level River St – 2014-04-21T15:44:44.730
3@steveverrill I don't believe [code-trolling] belongs here, nor do I believe it brings anything positive to the site - and for those two reasons I will downvote and vote to close all code-trolling questions. – Gareth – 2014-04-21T16:23:34.817
@Doorknob Too broad was a good close vote reason. Off topic was not. Thank you for voting correctly. – Justin – 2014-04-21T16:39:02.837
2@Quincunx In code-trolling, part of the fun is in exploring how broad the question is. I agree with Geobits. – 11684 – 2014-04-24T19:24:23.060
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots – phuclv – 2014-04-27T01:29:36.470
2
@Gareth That seems to me to be an incredibly immature approach. The community at large clearly prefers to keep code-trolling questions, and trying to be a vocal minority and shout down everyone who enjoys it is not the way to go. If you hate it so much, add it to your ignored tags, but don't try to force your opinion on the rest of the site. You've already lost that vote.
– Chris Hayes – 2014-04-30T02:01:18.343@qwr I think this has been around long enough to justify accepting an answer :-) – Justin – 2014-05-07T18:45:37.310
Code-trolling is in the process of being removed, as per the official stance. This question is very highly voted with many answers, many of which are extremely highly voted. It recieved just over 50% "delete" votes on the poll, but it is unique in that it recieved so many answers and votes, so I am locking it for historical significance.
– Doorknob – 2014-05-11T23:07:14.463