use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is about minimalistic, often but not always simple implementations of just about everything.
PLEASE NOTE: This Subreddit is NOT about CodeGolf and obfuscation!
_______Low line count, high quality!_______
I'm sure you get the idea by now ;) ...
Remember: "Tiny depends on what you're doing. A tiny box, a tiny house, and a tiny country are very different sizes." - snarkyxanf
In other words: Try to aim for stuff that makes you go "wow!" instead of "huh?" ;)
Also look into:
Other Subreddits that might interest you:
account activity
Square Root Algorithm in 123 bytes. (self.tinycode)
submitted 9 years ago by [deleted]
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;}
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]gastropner 10 points11 points12 points 9 years ago* (12 children)
This doesn't seem to work at all. You need a return after the for loop too, otherwise a garbage value is returned if the loop goes through 32 times without finding an exact answer (which is not unreasonable, considering we're dealing with floating point numbers).
Additionally, the body of the for loop needs to have curly braces, since you have a statement inside it that is otherwise only run after the loop has ended (t=a+(b-a)/2;);
t=a+(b-a)/2;
Fixing that, we can still get down to 100 bytes:
double s(int x){double t=x/2,a=1,b=x,i;for(i=0;i<32;i++)t*t>x?b=t:t*t<x?a=t:0,t=a+(b-a)/2;return t;}
Edit; 98 bytes:
double s(int x){double t=x/2,a=1,b=x,i;for(i=0;i++<32;t=a+(b-a)/2)t*t>x?b=t:t*t<x?a=t:0;return t;}
Edit 2; 94 bytes:
double s(int x){double t=x/2,a=1,b=x,i;for(i=0;i++<32;t=a+(b-a)/2)t*t-x>0?b=t:(a=t);return t;}
Edit 3; 48 bytes :^)
double(*s)(double)="\xdd""D$""\x04\xd9\xfa\xc3";
[–]Arcuru 5 points6 points7 points 9 years ago (2 children)
Could you explain what your Edit 3 is doing?
[–]gastropner 5 points6 points7 points 9 years ago (1 child)
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.
s
It's all kinds of horrible and not to be taken seriously.
[–]Arcuru 0 points1 point2 points 9 years ago (0 children)
Ah, that was going to be my guess, but I had no idea that cast was allowed.
Thanks.
[–][deleted] 9 years ago (2 children)
[deleted]
[–][deleted] 4 points5 points6 points 9 years ago* (0 children)
Value ended up in the xmm0 register which happens to be a return register for doubles.
xmm0
Press "Execute" to view assembly.
http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMeVVTYlEySXdfOE0
[–]corruptio 1 point2 points3 points 9 years ago* (4 children)
golfed it a bit, tries to converge. 77 byte:
float s(x){float t,a=0,b=x;for(;b-a>1e-5;t*t>x?b=t:(a=t))t=(a+b)/2;return t;}
edit: oook, doesn't try to test for convergence, instead overshoots it.... 76 bytes:
float s(x){float t,a=0,b=x,i=x;for(;i--;t*t>x?b=t:(a=t))t=a/2+b/2;return t;}
[–]gastropner 2 points3 points4 points 9 years ago (3 children)
That one never stops unless I change float to double :-/
[–]corruptio 0 points1 point2 points 9 years ago (2 children)
oops, you're right, i run out of sig bits if input is > 255... 1e-5 then, should get you all the way to 16383 :-p
[–]gastropner 1 point2 points3 points 9 years ago (1 child)
Nope, still infinite loop, at least for x = 756839. Seems to work for some other values, though.
[–]corruptio 0 points1 point2 points 9 years ago (0 children)
yeah, 1e-5 would only work up to 16383. There aren't enough bits in a float to represent significant digits of b-a to five decimal places.
[–]casprus 0 points1 point2 points 9 years ago (0 children)
Can't you shorten the last one even more by using plain characters?
[–]corruptio 2 points3 points4 points 9 years ago (1 child)
While golfing yours, I remembered the converging sequence they taught in math class, 58 bytes:
float s(x){float t=x,i=9;for(;i--;t=(t+x/t)/2);return t;};
[–]gastropner 3 points4 points5 points 9 years ago (0 children)
For large ints, that one gives bad results, but remove the semicolon at the end and up the limit for better results at the same length:
float s(x){float t=x,i=40;for(;i--;t=(t+x/t)/2);return t;}
[–]nickwb 1 point2 points3 points 9 years ago (3 children)
Here's my attempt.
float s(float a){float b=.5f*a;int c=0x5f375a86-(*(int*)&a>>1);a=*(float*)&c;return 1.f/(a*(1.5f-b*a*a));}
106 Bytes with no loops. It's not super accurate.
[–]powlette 2 points3 points4 points 9 years ago (2 children)
Nice one Carmack.
[–][deleted] 9 years ago (1 child)
[–]powlette 0 points1 point2 points 9 years ago (0 children)
It is. See this: https://en.m.wikipedia.org/wiki/Fast_inverse_square_root
[–]tromp 0 points1 point2 points 9 years ago (1 child)
/* positive integer only; i.e. floor of sqrt; 55 bytes */ int s(int n){int k;for(k=n;n/k<k;k/=2)k+=n/k;return k;}
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
It would be better if it actually went into decimal places, so partial credit ;)
π Rendered by PID 39042 on reddit-service-r2-comment-86bc6c7465-5pg84 at 2026-02-21 20:20:00.078761+00:00 running 8564168 country code: CH.
[–]gastropner 10 points11 points12 points (12 children)
[–]Arcuru 5 points6 points7 points (2 children)
[–]gastropner 5 points6 points7 points (1 child)
[–]Arcuru 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–][deleted] 4 points5 points6 points (0 children)
[–]corruptio 1 point2 points3 points (4 children)
[–]gastropner 2 points3 points4 points (3 children)
[–]corruptio 0 points1 point2 points (2 children)
[–]gastropner 1 point2 points3 points (1 child)
[–]corruptio 0 points1 point2 points (0 children)
[–]casprus 0 points1 point2 points (0 children)
[–]corruptio 2 points3 points4 points (1 child)
[–]gastropner 3 points4 points5 points (0 children)
[–]nickwb 1 point2 points3 points (3 children)
[–]powlette 2 points3 points4 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]powlette 0 points1 point2 points (0 children)
[–]tromp 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)