24
2
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.
24
2
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.
31
Technically not a library function :)
input()**.5
As a function it's 16 chars
f=lambda x:x**.5
I think the function has to be called 's', not 'f'. – DoctorHeckle – 2015-07-08T17:56:34.213
2It's questionable whether this falls under a built-in function (it calls __pow__
). But then even *
is a built-in. – marcog – 2011-01-29T11:37:06.390
Would have preferred it as a function, but I'll give you the accept for the ingenuity. – jtjacques – 2011-01-31T15:45:04.430
19@jtjacques What ingenuity? IMHO, this is the one the most obvious ways around the rules. – Peter Olson – 2011-05-16T03:48:04.463
Smart, I like it. – jtjacques – 2011-01-28T01:25:25.280
24
f=.5.__rpow__
This is equivalent to f=lambda x:x**.5
, but 3 bytes shorter.
17
Takes a while to run for large numbers :)
n=input()
i=0
while i*i<n:i+=1e-9
print i
1For me, it takes a while even for 2. – nyuszika7h – 2014-04-30T13:44:08.050
14
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);
9
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.
8
5
LISP (66)
Using Babylonian method.
(defun s(A)
(do((x 1(*(+ x(/ A x)).5))(n 100(1- n)))((zerop n)x)))
4
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
4
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.
You can save 8 characters, and allow calling s(n)
(at least in GCC), by changing s(int n,int g)
to s(n,g)
. – Joey Adams – 2011-03-18T04:20:09.603
Save 2 more chars by changing random()
to rand()
. – Paul R – 2011-06-14T14:36:55.827
4
Naive solution, accepts only positive integer inputs.
s n=foldr (\x a->x*x==n||a) False [1..]
Haskell, 39 characters.
6remove spaces? s n=foldr(\x a->x*x==n||a)False[1..]
– Ming-Tang – 2011-01-28T03:51:36.633
3
Uses Heron's 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);
3
return 1.f/InvSqrt(x);
InvSqrt of course courtesy of Quake.
What, you mean you wanted an accurate result?
Well to the limit of the return type, and also one which does not use a library. – jtjacques – 2011-01-28T01:08:38.960
3
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
3And the size is ... – user unknown – 2011-11-08T05:35:10.603
2
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.
2
Iterative (implementation of 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.
2
f64 s(f64 x){return exp(log(x)/2);}
I promise I didn't use sqrt()
!
2
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 :(
2
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}
You're missing the function name there. Should be something like function s(n)
. – nyuszika7h – 2014-04-30T10:41:39.040
@nyuszika7h: It’s a function literal. – Ry- – 2014-04-30T14:28:54.453
But you would need to assign that to a variable (which is the same "penalty" as using my previous suggestion). I don't see Python solutions like lambda x: ...
either, I see solutions like f=lambda x: ...
. – nyuszika7h – 2014-04-30T14:30:14.933
@nyuszika7h: Their loss. This code is a function. – Ry- – 2014-04-30T14:30:37.530
I'll let the meta "gods" decide, since I've never seen anyone else doing this, as far as my memory goes back. http://meta.codegolf.stackexchange.com/q/1501/344
– nyuszika7h – 2014-04-30T14:37:54.893@nyuszika7h: There, it’s a variable now – Ry- – 2014-04-30T14:44:37.077
Heh, did you convert it to ES6 Harmony code? ;) – nyuszika7h – 2014-04-30T14:45:53.163
@nyuszika7h: =) Now if only Chrome would follow suit. – Ry- – 2014-04-30T14:46:37.620
1Also, sorry if it came off as being rude, I didn't mean to be. – nyuszika7h – 2014-04-30T14:46:58.353
1
Mathematica 27
f@x_:=Nest[(#+x/#)/2&,1.,9]
1
i`Ad`A^.5
As a function, 15 characters:
i`A:Return A^.5
1
function s(n){return Math.pow(n,.5)}
1
AWK, 8
1,$0^=.5
Following solution can handle all values except 0
AWK, 6
$0^=.5
1
&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+
1
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:
>+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:
>+>>>>+++++++++++++++<<<<[>>+<+<[->[->->>+<<]>[-<+>>]<+<<]>[-]>[-]<<<[->+>>+<<<]>[-<+>]>>>>[-<+<<<+>>>>]<<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>>>[-<<<+>>>]<[->>+>+<<<]<[->+>>>>+<<<<<]>+[>>[-<]<[>]<-]>>>+<[[-]>[-]>[-<<<<+>>>>]<<<<<+<+>>>>]>[->[-]<[-<<<+>>>]]<<<[->+<]<<[->>+<<<<->>]<+<[-[+>>>[-]>>[-]<<<<-]>[->>>>>+<<<<]<]>[->]<<]<[-]>>>>>[-<<<<+<+>>>>>]>>[-<<<<<<+>>>>>>]<[-<<<+>>>]<<[-<<+>>]
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[->-[>+>>]>[+[-<+>]>+>>]<<<<<]}
0
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
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.
0
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
0
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 ;-)
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/')
}
The portable version seems to work fine in zsh
too. – manatwork – 2013-12-16T08:31:07.673
@manatwork Thanks I'v forgot them! – F. Hauri – 2013-12-16T08:36:14.503
0
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)?sp
and ?sn
asks for input and stores it to the registers1
is the original seedddstlnr/+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
.0
s=(n)->n**.5
Thanks to gnibbler's post for the .5
idea, I wouldn't have remembered that by myself.
0
f(x) = x^.5
0
It seems like cheating since other languages has similar solutions. But it works.
let s n=n**0.5
0
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
0
2ˣ√Ans
If that's invalid:
Ans^.5
If that's also invalid:
e^(.5ln(Ans
If that's also invalid:
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 – 2015-07-08T01:34:55.613
0
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 – 2015-11-29T02:55:30.987
@BradGilbertb2gills If you still cares about this, Seriously has its own code page. – user202729 – 2018-01-21T15:55:31.803
0
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 – 2015-11-28T22:39:50.550
1@JohnE Excellent point, added. – Addison Crump – 2015-11-28T22:40:32.237
0
-2
/*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;
}
3This needs to be community wiki. – Timtech – 2014-04-29T10:40:19.017
1Welcome 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 – 2014-04-29T11:28:05.047
-2
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]);
}
}
6How do you feel about
exp(0.5*log(x))
? – dmckee --- ex-moderator kitten – 2011-01-28T02:07:45.1034The 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 – 2011-01-28T02:11:10.227
btw. It's reimplementing – Alexandru – 2011-01-28T02:18:07.423
@dmckee - It does not break the spirit of the rules, but it doesn't give you a square root. – jtjacques – 2011-01-28T02:21:12.190
2@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 – 2011-01-28T02:23:25.580
@eBusiness I thought "as accurate as possible" would be implied and I think we've established that x^0.5 is probably going to be the best solution. – jtjacques – 2011-01-28T02:23:58.333
@dcmckee, showing my ignorance I did, using Google. What should I be doing to get 2? http://www.google.com/search?q=exp(0.5*log(4))
– jtjacques – 2011-01-28T02:30:33.570That really kills the task, if this is to be fun you got to set the restrictions tight enough. – aaaaaaaaaaaa – 2011-01-28T02:30:47.480
2@jtjacques: Base agreement is key, many languages use
log
for the base 10 logarithm, so you might tryexp(0.5*ln(x))
to get the natural log orpow10(0.5*log(x))
or similar. – dmckee --- ex-moderator kitten – 2011-01-28T02:38:54.173@dcmckee Thanks. I apologise for discrediting/besmirching your solution. – jtjacques – 2011-01-28T02:41:01.020
@jtjacques: Actually I haven't made a answer of it because I don't like it. The reason you wouldn't have a
sqrt
available it that you don't have support for transcendental functions, which makes usingexp
andlog
a little silly to my mind. – dmckee --- ex-moderator kitten – 2011-01-28T02:55:35.6971How is this a duplicate? Can you check the date it was asked before closing? (nominated for re-opening) – Erik the Outgolfer – 2016-06-20T13:57:10.973
@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ An old question can be closed as a duplicate of a new one.
– James – 2016-06-20T16:27:31.943@Alexandru That was exactly the algorithm that came to mind when I first saw the brief... Great minds think alike, so I gave you +1 – WallyWest – 2013-12-16T00:19:41.263
@dmckee Are you sure?
– Mateen Ulhaq – 2011-04-30T06:43:19.2433
I think all solutions will be based on Newton approximation such as http://www.codemaestro.com/reviews/9
– Alexandru – 2011-01-28T00:56:08.587That doesn't prevent it being golfed, nor does it dictate how to handle any unusual cases, such as negative numbers. – jtjacques – 2011-01-28T01:10:31.597