# Reimplementing square root [duplicate]

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.

• I think all solutions will be based on Newton approximation such as codemaestro.com/reviews/9 – Alexandru Jan 28 '11 at 0:56
• How do you feel about exp(0.5*log(x))? – dmckee --- ex-moderator kitten Jan 28 '11 at 2:07
• The problem with this puzzle is that it lacks specifications for input range and tolerated error, it's kinda boring if "keep on adding a small number until result is reached" method is allowed, it's the shortest in any language. – aaaaaaaaaaaa Jan 28 '11 at 2:11
• @jtjacques: Er...yes it does. Try a couple of cases. Make sure that you use the same base for exponentiation and logarithm. – dmckee --- ex-moderator kitten Jan 28 '11 at 2:23
• @jtjacques: Base agreement is key, many languages use log for the base 10 logarithm, so you might try exp(0.5*ln(x)) to get the natural log or pow10(0.5*log(x)) or similar. – dmckee --- ex-moderator kitten Jan 28 '11 at 2:38

# TI-BASIC, 3

2ˣ√Ans


If that's invalid:

## 4

Ans^.5


If that's also invalid:

## 5

e^(.5ln(Ans


If that's also invalid:

## 21

Babylonian method.

Ans→N
While Ans²≠N
mean({Ans,N/Ans
End
Ans

• Thia challenge is too simple, broken, and four years old, but there's no harm in answering. – lirtosiast Jul 8 '15 at 1:34

## F# (12)

It seems like cheating since other languages has similar solutions. But it works.

let s n=n**0.5


# Julia (9) characters

f(x) = x^.5


# Vitsy, 5 Bytes

This language is 4 years newer than the challenge. Go figure.

Not so hard...

12/^N
12/      Push 1/2.
^     Put the input to the power of 1/2.
N    Output as number.
• Might be worth noting that Vitsy was invented about 4 years after this challenge was posed. – JohnE Nov 28 '15 at 22:39
• @JohnE Excellent point, added. – Addison Crump Nov 28 '15 at 22:40

## Seriously, 4 bytes

This language is 4 years newer than this challenge

1½,^


This does the following Python code in Seriously:

n=input();n**.5

• That's 4 characters, but is 5 bytes – Brad Gilbert b2gills Nov 29 '15 at 2:55

## Perl 6,  13  12 chars

sub s{\$^a**½} # 13 chars / 14 bytes

my &s=* **½; # 12 chars / 13 bytes

 /*Fast inverse square root*/
/* from http://en.wikipedia.org/wiki/Fast_inverse_square_root */
float rsqrt( float y,int iter)
{
const unsigned int fpMagic = 0x5f3759de << 1;
/*evil floating point bitlevel hacking*/
unsigned int *i;
if (sizeof(int) != sizeof(float))
return -1.0;
int j;
const float threehalfs = 1.5F;
float x2 = y * 0.5F;
void *p = &y;
i = p;
*i = (fpMagic - *i) >> 1;
/* Newton iterations */
for (j=0; j < iter; j++)
{
y  *=  threehalfs - ( x2 * y * y ) ;
}
return 1.0 / y;
}

• This needs to be community wiki. – Timtech Apr 29 '14 at 10:40
• Welcome to the site! This is a code-golf challenge, which means you should try to shorten the code as much as possible - use one-letter variable names and remove unneeded whitespace. – nyuszika7h Apr 29 '14 at 11:28

Java: 74 characters

I'm leaving the square root as an exact answer because I don't enjoy approximations :)

enum A{;public static void main(String[]a){System.out.println('√'+a);}}


or

enum A {

;public static void main(String[] a) {
System.out.println('√' + a);
}
}