Python 2.7
I quake in the presence of my betters. Their answers are incredible. However, I feel your learning will be incomplete if you do not see an algorithmic implementation of a square root method so simple that people have used it by hand. Wikipedia explains this algorithm.
I have provided the following Python code as a sample implementation of the algorithm. While Python is slower than Java (which is slower than ASM, which is slower than C), I have found that this is because the computer is being more careful with its calculations. Combining this with Python's great readability, I believe you should use Python for every homework assignment. If your professor tells you to use a different language, explain to him why he is wrong.
Here is this alternative square root approach:
def sqrt(z):
v,w,y=10,0,100
def A(x):
a,b=D(x),[];c=x-a
while a>w:b.insert(w,a%y);a//=y
b.append(None)
while c>w:c*=y;x=D(c);b.append(x);c-=x
return b
B=lambda x:reduce(lambda x,y:x if y is None else x*v+y,x,w)
C=lambda x,y:(x+y)*y
D=int
a,b,c,d= w,[],-1,A(z);e=len(d);f=e+y
while c+1<f and(c<e or a>w):
c += 1
if c<len(d) and d[c]is None:b.append(None);continue
a*=y
if c<e:a+=d[c]
g=B(b)*20;h=max(filter(lambda c:C(g,c)<=a,range(v)));b.append(h);a-=C(g,h)
return float(''.join(map(lambda x:'.' if x is None else str(x),b)))
Here are some results showing what it produces:
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
> > from random import *
> > from golfedSqrt import sqrt
> > digits = [i for i in range(11)]
> > digits.extend([random() * 1e9 + random() for i in range(10)])
> > for d in digits:
... print "{} => {}".format(d, sqrt(d))
...
0 => 0.0
1 => 1.0
2 => 1.41421356237
3 => 1.73205080757
4 => 2.0
5 => 2.2360679775
6 => 2.44948974278
7 => 2.64575131106
8 => 2.82842712475
9 => 3.0
10 => 3.16227766017
216308371.652 => 14707.4257317
556847164.007 => 23597.6092858
106003255.816 => 10295.7882562
824923809.742 => 28721.4868999
204798557.219 => 14310.7846472
742647120.414 => 27251.5526239
199156541.747 => 14112.283364
788130088.331 => 28073.6547021
525449922.652 => 22922.6944893
529451788.497 => 23009.819393
> >
I believe the virtues of this approach are self-evident thanks to Python's strengths.