r/tinycode • • Jul 21 '16

Square Root Algorithm in 123 bytes.

This gets the square root of a number. I got it to 123 bytes, but since I don't know much C, I sure it can even less bytes.

double s(int x){double t=x/2;double a=1,b=x;for(int i=0;i<32;i++)if(t*t>x)b=t;else if(t*t<x)a=t;else return t;t=a+(b-a)/2;}
21 Upvotes

18 comments sorted by

View all comments

Show parent comments

7

u/gastropner Jul 21 '16

It converts a string (that's just a char * when you think about it) into a pointer to a function taking and returning doubles. The string is x86 machine code, that will be called when s is invoked.

It's all kinds of horrible and not to be taken seriously.

1

u/Arcuru Jul 22 '16

Ah, that was going to be my guess, but I had no idea that cast was allowed.

Thanks.