Came across this thread on stack exchange. The challenge was to write a code, using whatever language you want, that would print the number 2014 to the console, without using a single numeral in the code. The person to post the code with the least number of characters wins.
After scrolling a whole lot over a number of Befunge codes and all, I came across a JS code, where the guy had said “Using pure-Math only..”, _and he’d posted two codes, one used some type-conversion while the _older version was like in plain JS. This:
Since the guy had written that the code was pure-math, I decided to give it a closer look.
As I was going to try it out, I opened another tab, and in the console, I pasted his code. It did really print out 2014, but how? It’s been ages since I even printed my own name in JS, so figuring out the code took me quite a while.
In the address bar i typed “data:text/html, <html contenteditable>” to make it editable, as i wanted to make notes while trying shit in the console.
So, the first thing I did was add a little whitespace to the code and make it slightly more presentable.The code is basically 4 lines, should i say? Notice the three semicolons? yes, they ensure that the code wont screw up if you give a line break right after the semicolon.
The first bit is “m=Math”. This the guy did simply to save space and perhaps for his own ease. Math.function() is the syntax for performing and implementing in your code various mathematical functions, rather like the Math module in Python. Followed by “p=m.pow”, and “t=true.” m.pow is his code’s version of Math.pow(x,y), which sets the index of parameter x to y, so Math.pow(2,3) would be equal to 2³. This too was merely to save space. The t = true could be confusing for some. That’s when JSs automatic type-conversion comes in handy, (True = 1; 0 = False). So “t-true” sets “t=1.”
Now the main expression:
The ++t sets the variable t= t+1. I’ts the same as t += 1 in Python. You might have noticed that the code also contains “t++“, so what’s the difference between the two?
t++ _increases ‘t’ by 1, but does not implement therein. Means if the code is _t=3; t++*3; _The first bit set’s ‘t’ equal to 3, and in the second, t is multiplied by 3 while at the same time increased by one. ++t is slightly different. It increases and implements right then. So _++t*3 would result in 9 not 6.
Then the m.floor function is used to round off a number to the last integer. So 5.9 would become 5. Math.exp(a) results in 10ª, while Math.PI is a constant, which of course is equal to 3.141592653589793.
That’s pretty much all the functions explained. After starting with the very _deepest _bracket, and working my way out to the end, I ended up with the following expression that’s equal to ‘2014’, and is like a simplified version of the original code. this:
To see how I got there, here’s a copy of the page on which I was making all the notes.