My blog has been moved to

Friday, March 26, 2010

multiples of 3 or 5

One day Helder showed me Project Euler, a collection of interesting math problems to be solved using computer programs.

I took a look at the first problem: find the sum of all the multiples of 3 or 5 below 1000. Getting the linear time solution was trivial, the constant time solution was also not hard. However, I could not help it, I continued by applying some obfuscation voodoo and came up with this answer (of course, constant time as well):

return "uncopyrightable"[n % 15] - 'a' + 44 - "xzoxy}pge]_LAKD"[n% 15] +
'A' - (!(n % 15)) * 9 + 15 * ((n / 15) * (4 + ("aaabbcdddeffggg"[n % 15] - 'a')) +
(n / 15) * (n / 15 - 1) * 7 / 2);

How did a word ("uncopyrightable") end up in that solution? Believe me, it was (a lot of) fun! :)


Anonymous said...

Sorry Ariya,

Don't mean to be critical but
I get an answer of 234168 which is wrong I'm afraid. Close but wrong. I'm calling a function that just contains your return statement with a vaue for n of 1000.


Ariya Hidayat said...

Hi Brad,

You have to pass 999 to that function, than you should get 233168 (the correct answer).

The problem statement says "below 1000". Sorry if I did not make it clear in my solution.

Tomaz said...

I looked, stared, stared a bit more, then, finally understood. wich reminds me, I do need a girlfriend =P

Anonymous said...

Hi Ariya,

My bad, should've tried that. Love reading your code and trying out the examples (loved the graphics dojo stuff as well). Any chance of some QML examples (specifically data binding and dynamic object creation)?

Just a thought. Keep pumping out content :)


Ariya Hidayat said...

Not sure I have some ideas for QML, but I keep that in mind.