24
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ I think all solutions will be based on Newton approximation such as codemaestro.com/reviews/9 \$\endgroup\$ – Alexandru Jan 28 '11 at 0:56
  • 6
    \$\begingroup\$ How do you feel about exp(0.5*log(x))? \$\endgroup\$ – dmckee --- ex-moderator kitten Jan 28 '11 at 2:07
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – aaaaaaaaaaaa Jan 28 '11 at 2:11
  • 2
    \$\begingroup\$ @jtjacques: Er...yes it does. Try a couple of cases. Make sure that you use the same base for exponentiation and logarithm. \$\endgroup\$ – dmckee --- ex-moderator kitten Jan 28 '11 at 2:23
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – dmckee --- ex-moderator kitten Jan 28 '11 at 2:38

38 Answers 38

1
2
0
\$\begingroup\$

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
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thia challenge is too simple, broken, and four years old, but there's no harm in answering. \$\endgroup\$ – lirtosiast Jul 8 '15 at 1:34
0
\$\begingroup\$

F# (12)

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

let s n=n**0.5
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Julia (9) characters

f(x) = x^.5
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

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.
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Might be worth noting that Vitsy was invented about 4 years after this challenge was posed. \$\endgroup\$ – JohnE Nov 28 '15 at 22:39
  • 1
    \$\begingroup\$ @JohnE Excellent point, added. \$\endgroup\$ – Addison Crump Nov 28 '15 at 22:40
0
\$\begingroup\$

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
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ That's 4 characters, but is 5 bytes \$\endgroup\$ – Brad Gilbert b2gills Nov 29 '15 at 2:55
  • \$\begingroup\$ @BradGilbertb2gills If you still cares about this, Seriously has its own code page. \$\endgroup\$ – user202729 Jan 21 '18 at 15:55
0
\$\begingroup\$

Perl 6,  13  12 chars

sub s{$^a**½} # 13 chars / 14 bytes
my &s=* **½; # 12 chars / 13 bytes
|improve this answer|||||
\$\endgroup\$
-2
\$\begingroup\$
 /*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;
}
|improve this answer|||||
\$\endgroup\$
  • 3
    \$\begingroup\$ This needs to be community wiki. \$\endgroup\$ – Timtech Apr 29 '14 at 10:40
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – nyuszika7h Apr 29 '14 at 11:28
-2
\$\begingroup\$

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[0]);}}

or

enum A {

    ;public static void main(String[] a) {
        System.out.println('√' + a[0]);
    }
}
|improve this answer|||||
\$\endgroup\$
1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.