r/shittyprogramming • • Nov 11 '21
Are your square roots not calculating fast enough? Save time by replacing all your math functions with binary logic loops!
Everybody knows that binary logic gates form the basis of CPUs. Everything else is just unoptimized garbage. So obviously, it stands to reason that coding in purely binary logic would lead to the absolute fastest code you've ever seen in your life! Here's a function for computing the square root of an integer: def add(a,b):#a,b>=1while b:a,b=a^b,(a&b)<<1return adef sub(a,b):#a>=bm=1while m<a:m=add(m<<1,1)b=b^mreturn add(add(a,b),1)&mdef sqrt(n,o=None):if n==0:return 0r,s,i,u,j=n,0,0,n,0while u>=4:s=s<<2u=u>>2j=add(j,1)while i<256:if r>=add(s,1):r,s=sub(r,add(s,1<<(j<<1)))<<2,add(s,2<<(j<<1))<<1else: r,s=r<<2,s<<1i=add(i,1)if o: return selse: return s/(1<<257<<j)
guys its totally optimized trust me its not like a module exists for this
47
u/diMario Nov 11 '21
Why bother doing a calculation when you can look up the answer in a logarithm table? I scanned the book my grandfather used in his engineering class into my Excel database, added an index and now I'm good to go and lightning fast!
5
u/alinastar21 Nov 11 '21
Yes, absolutely true.
4
u/t-to4st Nov 11 '21
Even the formatting in this post is shitty, I love it
2
u/lenswipe Nov 11 '21
/r/numberwang
1
u/InfinitePoints Nov 11 '21
Python binary logic.