See if you can figure out what this code does before running it (shouldn't be too hard)...
I was messing around today with some code and I wrote a horrendously disgusting function that does an elementary task in the nastiest way imaginable. See the ugly code for yourself (written in VC++ 6.0):
Ok, so I cheated with the two's complement there at the end, but for some reason there's something rather satisfying about such a gut-wrenching algorithm that does a zillion times work work than is necessary. I just wanted to share the agony
Anyone else have any submissions? Nauseating algorithms to execute simple tasks that you'd like others to try and figure out on their own? I'd like to see the revolting, miasmatic algorithms that we at Ars can conceive
I was messing around today with some code and I wrote a horrendously disgusting function that does an elementary task in the nastiest way imaginable. See the ugly code for yourself (written in VC++ 6.0):
code:<HR>Code:#include <iostream> #include <map> #include <math.h><P>using namespace std;<P>map<long,long> m;<P>long func(long a, long b);<P>int main() { for(int x = 0;x <= 9;x++) m[x] = 9 - x;<P> cout << func(0,1);<P> return 0; }<P>long func(long a, long b) { long d = (long)floor(log10((double)b)) + 1, val = 1 + a, t = 1; for(long x = 0;x < d;x++,t*=10) val += (m[(b % (t * 10)) / t] * t); return val > t / 2 ? ((t ^ 0xffffffff) + 1) + val : val; }
Ok, so I cheated with the two's complement there at the end, but for some reason there's something rather satisfying about such a gut-wrenching algorithm that does a zillion times work work than is necessary. I just wanted to share the agony
Anyone else have any submissions? Nauseating algorithms to execute simple tasks that you'd like others to try and figure out on their own? I'd like to see the revolting, miasmatic algorithms that we at Ars can conceive