[Question]
  [
**This question already has an answer here**:
 
 
[Find the square root, BUT [closed]](/questions/71477/find-the-square-root-but)
 (1 answer)
 
Closed 8 years ago.
Define a function, s, which takes a number and returns the square root.
No use of library functions, such as Java's Math.sqrt() or PHP's built in sqrt(), allowed.
  
[Answer]
## Python - 11 chars
Technically not a library function :)
```
input()**.5
```
As a function it's 16 chars
```
f=lambda x:x**.5
```
[Answer]
## Python (13 chars)
`f=.5.__rpow__`
This is equivalent to `f=lambda x:x**.5`, but 3 bytes shorter.
[Answer]
## Python - 41 chars
Takes a while to run for large numbers :)
```
n=input()
i=0
while i*i<n:i+=1e-9
print i
```
[Answer]
## Java (163)
Implementing a double precision square root calculator by making use of the fast invert square root stuff from quake and a new constant for the 64bit floats. In java. Yay for verbosity.
```
public double i(double a){double b=a/2;long c=0x5fe6ec85e7de30daL-(Double.doubleToRawLongBits(a)>>1);a=Double.longBitsToDouble(c);return a*(1.5-b*a*a);};s=1/i(x);
```
[Answer]
## **J, ~~6~~, 5**
Using power:
```
^&0.5
```
Or, perhaps more mnemonically:
```
^&1r2
```
Using the slightly less "cheaty" method, `exp(log(x)/2)`. 
```
-:&.^.
```
Except since `exp` is the inverse of `log`, we simply "**halve** (`-:`) **under** (`&.`) **log** (`^.`)"
Not that normally a J programmer would not name a method this short; he'd simply embed in in a larger program.
[Answer]
# Haskell, 9 characters
```
s=(**0.5)
```
Similar to [@gnibbler's Python solution](https://codegolf.stackexchange.com/questions/73/reimplementing-square-root/83#83).
[Answer]
**LISP (66)**
Using Babylonian method.
```
(defun s(A)
   (do((x 1(*(+ x(/ A x)).5))(n 100(1- n)))((zerop n)x)))
```
[Answer]
Naive solution, accepts only positive integer inputs.
```
s n=foldr (\x a->x*x==n||a) False [1..]
```
Haskell, 39 characters.
[Answer]
## Python - 65
A simple solution using Newton's method.
```
def s(x):
 t=1.0
 while 1e-9<abs(x-t*t):t-=(t*t-x)/2/t
 return t
```
[Answer]
C with some bad form and deprecated features:
```
s(int n,int g){return g*g-n?s(n,random()%n):g;}
```
If your compiler is enough of a liberal hippie, it should be possible to call s(n) and (eventually) receive the desired value.
[Answer]
```
return 1.f/InvSqrt(x);
```
InvSqrt of [course courtesy of Quake](http://www.beyond3d.com/content/articles/8/).
What, you mean you wanted an accurate result?
[Answer]
```
def sqrt_newton(x):
 f,g,w,d=lambda a:a*a-x,lambda a:2.0*a,x/2.0,1e-4
 while abs(f(w))>d:w-=(f(w)/2.0/w)
 return w
```
reduced version of <https://gist.github.com/713104>
[Answer]
## C++ (61)
*Uses [Heron's Method](http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method):*
```
f64 s(f64*x,u64 n=9){*x=(x[1]/*x+*x)/2;return n?s(x,--n):*x;}
```
Recursion, pointers, optional arguments, and ternary operators FTW!
---
Usage:
```
f64 x[2] = {12.0, 12.0};
std::cout << s(x);
```
[Answer]
## C++ (35)
```
f64 s(f64 x){return exp(log(x)/2);}
```
I promise I didn't use `sqrt()`!
[Answer]
## ActionScript3 (53)
Using Newton's method. (Hooray 4 IEEE 754)
```
function s(b,d=2){return b==d*d?d:s(b,d-(d*d-b)/2/d)}
```
I know I'm TOO late but I just wanted to write something I did :(
[Answer]
# GolfScript - 21
It uses the Babylonian method and works only with integers. According to my tests, it's good for up to 56 digits.
```
{1{.2$\/+2/}99*\;}:s;
```
Usage: `1000 s` -> `31`
I think this is the shortest solution so far that doesn't call some kind of power function or external library.
[Answer]
# k (17 chars)
Iterative (implementation of [Babylonian method](http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)):
```
{{.5*y+x%y}[x]/x}
```
Iterates until two successive values are *equal*
Example:
```
sqrt[1234] = {{0.5*y+x%y}[x]/x}1234
1b
```
Also the mandatory `xexp[;0.5]` for 10.
[Answer]
# JavaScript, 121
No, it doesn't even come close, but it is more optimal than other solutions and doesn't use `Math.pow` with fractions.
```
s=n=>{for(var c=Math.pow(10,(''+Math.floor(n)).length),v=0,l;(l=v*v<n),c>1e-10;l!=v*v<n?c/=10:0)v*v<n?v+=c:v-=c;return v}
```
[Answer]
**Mathematica 27**
```
f@x_:=Nest[(#+x/#)/2&,1.,9]
```
[Answer]
## Golf-Basic 84, 9 characters
```
i`Ad`A^.5
```
As a function, **15** characters:
```
i`A:Return A^.5
```
[Answer]
## JavaScript, 36 (or 14 without the function)
```
function s(n){return Math.pow(n,.5)}
```
[Answer]
**AWK, 8**
```
1,$0^=.5
```
Following solution can handle all values except 0
**AWK, 6**
```
$0^=.5
```
[Answer]
# Befunge 98 - 18
```
&00pv  
0*::<+1vj!`g0
```
This **program** takes an input number from the user, and ends by pushing the integer square root on the stack (note that you said square root, but didn't specify whether floating point was necessary). Here is a function (well, closest thing to one) (requiring free access to cell `00`) (**15** chars):
```
00p::*00g`!jv1+
```
[Answer]
In these I expect the byte to be squared is in the current cell. It needs 8 cells to the right empty and it will have answers in the 4 cells starting from where the number was. These are:
1. integer result
2. a alternative result (would be the same or one higher if those multiplied is closer to the argument)
3. remainder
4. flag for negative remainder
# [Extended BrainFuck](http://sylwester.no/ebf/): 310
```
>+4>15+4<[>>+<+<[->[->->>+<<]>[-<+>>]<+<<]>[-]>[-]3<[->+>>+3<]>[-<+>]4>[-<+3<+4>]<<[->-[>+>>]>[+[-<+>]>+>>]5<]3>[-3<+3>]<[->>+>+3<]<[->+4>+5<]>+[>>[-<]<[>]<-]3>+<[[-]>[-]>[-4<+4>]5<+<+4>]>[->[-]<[-3<+3>]]3<[->+<]<<[->>+4<->>]<+<[-[+3>[-]>>[-]4<-]>[-5>+4<]<]>[->]<<]<[-]5>[-4<+<+5>]>>[-6<+6>]<[-3<+3>]<<[-<<+>>]
```
It turns into the following:
# BrainFuck: 390 (the same as above run through the compiler)
```
>+>>>>+++++++++++++++<<<<[>>+<+<[->[->->>+<<]>[-<+>>]<+<<]>[-]>[-]<<<[->+>>+<<<]>[-<+>]>>>>[-<+<<<+>>>>]<<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>>>[-<<<+>>>]<[->>+>+<<<]<[->+>>>>+<<<<<]>+[>>[-<]<[>]<-]>>>+<[[-]>[-]>[-<<<<+>>>>]<<<<<+<+>>>>]>[->[-]<[-<<<+>>>]]<<<[->+<]<<[->>+<<<<->>]<+<[-[+>>>[-]>>[-]<<<<-]>[->>>>>+<<<<]<]>[->]<<]<[-]>>>>>[-<<<<+<+>>>>>]>>[-<<<<<<+>>>>>>]<[-<<<+>>>]<<[-<<+>>]
```
It's Newtons method starting at guess 16 and goes downwards. It stops when the last iteration didn't make a different integer result. This is actualy from the `sqrt` macro of EBF since I use it to implement print-string operator `|`. Here's the part from EBF source ungolfed:
```
; sqrt ^0  uses  9 cells that need to be empty
; will be fuzzy after calling because of divmod
; returns ^0 result
;         ^1 same as ^0 or it increased by 1. typically will ^2*^3 be closerto the requested argument than ^2*^2
;         ^2 remainder
;         ^3 indicates if remainder is negative
{sqrt
  check for wrong usage !diff:diff
  :in:guess:temp:cur:div:mod:res:indicator
  @in
  $guess+     ;initial guess is 16, but
  $mod 15+    ; its in mod and incremented (rounded up)
  $guess(     ; worst case is 1 with 5 iterations
      $cur+$temp+
      $guess(-$temp[->-$mod+$cur]>[@cur-$temp+$div]$cur+)
      $temp(-)$cur(-)
      $in(-$guess+$cur+)
      $guess(-$in+)
      $mod(-$div+$guess+)
      $cur &roundivmod ; remainder wil now be in mod
      $mod(-$res+)
      $cur(-$mod+$guess-)
      $temp+
      $guess[-[+$div(-)$res(-)$temp-]>[-@temp$indicator+$cur]$temp]>[-@temp>]
  )
  $in(-)
  $mod(-$guess+$in+)
  $indicator(-$guess+)
  $res(-$cur+)
  $div(-$temp+)
  $in
  !indicator!res!mod!div!cur!temp!guess!in
}
;; helper macros
; roundivmod uses divmod and puts the rounded result in ^0 and indication of rounded in ^1
; and a remainder (which ^1 is an indication is either reduction or inrement) in ^2
{roundivmod
    &divmod @cur
    ; *0|n-rem|rem|res|
    >>>(-<<<+)
    <(->>+>+) make double copy of remainder
    ; res|n-rem|0|0|rem|rem
    <(->+>>>>+)
    ; res|0|n-rem|0|rem|rem|n-rem
    >+[>>[-<]<[>]<-]
    >>>+<(
       [-]>[-]
       >(-<<<<+)
       <<<<<+<+
      )
     >(-
        >[-]
        <(-<<<+)
      )
  end divide
}
; this does the divmod. compiler is fuzzy after so caller must fix position to calling
{divmod[->-[>+>>]>[+[-<+>]>+>>]<<<<<]}
```
[Answer]
### Scala 78 chars:
```
type D=Double
def q(x:D,g:D=9):D=if(math.abs(g*g-x)<.001)g else q(x,(g+x/g)/2)
```
invocation:
```
scala> q(12345)
res36: D = 111.10805572305925
```
[Answer]
# Python, 44
```
def s(x):t=1.;exec"t=(t+x/t)/2;"*99;return t
```
Tested with:
```
for x in [0, (3+5**0.5)/2, 1000, 10**5, 10**11]:
    print x, '->', s(x)
```
Output:
```
0 -> 1.57772181044e-30
2.61803398875 -> 1.61803398875
1000 -> 31.6227766017
100000 -> 316.227766017
100000000000 -> 316227.766017
```
# Python, 50 49
In case you like recursion:
```
s=lambda x,t=1.,r=99:r and s(x,t/2+x/t/2,r-1)or t
```
Same testing code, same output.
[Answer]
# Postscript ~~94~~ 89
Babylonian method. Iterates until successive values are equal.
```
s{dup 2 div{2 copy div 1 index add .5 mul
2 copy eq{exit}if exch pop}loop pop exch pop}def 
```
Commented:
```
/s{
    dup 2 div  % S x
    {  % S x
        2 copy div  % S x S/x 
        1 index add .5 mul  % S x (S/x+x)/2
        2 copy eq { exit } if  % S x (S/x+x)/2
        exch pop  % S (S/x+x)/2:->x
    }loop   
    pop exch pop
}def
```
[Answer]
## [bash](/questions/tagged/bash "show questions tagged 'bash'") 228 ( a lot but **pure** bash only! ;)
For the count:
```
x=(600000 200000);z=${x[$(($1&1))]} y=1;while [ $y -ne $z ] ;do y=$z;printf -v z "%u" $(((${y}000+${1}00000000000/${y})/2));printf -v z "%.0f" ${z:0:${#z}-3}.${z:${#z}-3};done;z=0000$z;printf "%.3f\n" ${z:0:${#z}-4}.${z:${#z}-4}
```
As a function:
```
sqrt() {
    local -a xx=(600000 200000)
    local x1=${xx[$(($1&1))]} x0=1
    while [ $x0 -ne $x1 ] ;do
    x0=$x1
    printf -v x1 "%u" $(( (${x0}000 + ${1}00000000000/${x0} )/2 ))
    printf -v x1 "%.0f" ${x1:0:${#x1}-3}.${x1:${#x1}-3}
    done
    x1=0000$x1
    printf "%.3f\n" ${x1:0:${#x1}-4}.${x1:${#x1}-4}
}
sqrt 2000000
1414.214
sqrt 1414214
1189.207
echo $((1189207000/1414214))
840
```
(Nota: **840 x 1189** is A0 paper size in milimeters ;-)
## The same, but working under [zsh](/questions/tagged/zsh "show questions tagged 'zsh'"), [dash](/questions/tagged/dash "show questions tagged 'dash'"), [ksh](/questions/tagged/ksh "show questions tagged 'ksh'"), [bash](/questions/tagged/bash "show questions tagged 'bash'") and [ash](/questions/tagged/ash "show questions tagged 'ash'") (busybox):
```
sqrt() {
    _val=$1
    set -- 6 2
    x1=$(eval echo \$$((1+(_val&1))))00000 x0=1
    while [ $x0 -ne $x1 ] ;do
    x0=$x1
    x1=000$(( (${x0}000 + ${_val}000000000/${x0} )/2 ))
    x1=$(printf "%.0f" $(echo $x1|sed 's/\(...\)$/.\1/'))
      done
    printf "%.3f\n" $(echo $x1|sed 's/\(...\)$/.\1/')
}
```
[Answer]
# dc, 46 chars
## [Babylonian method](http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) - iterative with given precision:
```
Fk?sp?sn1[ddstlnr/+2/pdlt-[0r-]sbd0>blp<a]dsax
```
Takes precision and `N` from stdin. Example invocation (first number is the precision, second number is N:
```
{ echo 0.0000001 ; echo 489 ; } | dc -e'Fk?sp?sn1[ddstlnr/+2/pdlt-[0r-]sbd0>blp<a]dsax'
```
It prints all the iterations, just for fun. Printing only last one is trivial change - just move the `p` behind `/+2/` at the end of the program.
Explanation:
* `Fk` sets precision to 15 (see the [hexa trick here](https://codegolf.stackexchange.com/a/17050/2800))
* `?sp` and `?sn` asks for input and stores it to the registers
* `1` is the original seed
* the iteration is computed by `ddstlnr/+2/`. Then, `dlt-` computes difference from previous iteration. `[0r-]sbd0>b` performs absolute value of the difference, and if it is greater than the specified precision, loop continues - `lp<a`.
[Answer]
# CoffeeScript (12)
```
s=(n)->n**.5
```
Thanks to [gnibbler](https://codegolf.stackexchange.com/users/95/gnibbler)'s post for the `.5` idea, I wouldn't have remembered that by myself.
]