How programmers multiply

This afternoon, a discussion about base64 strings prompted the question “How much is 64^2?”. I didn’t have a calculator in hand, and I figured I should be able to calculate 64*64 in my head. The thought process went something like this:

64 is 256/4, and I know by heart that 256 = 2^8, and 4 = 2^2, thus 64 = 2^(8-2) = 2^6.

By the same method, 2^6 * 2^6 = 2^(6+6) = 2^12.

I have memorized a few more powers of two besides 256, and the easiest to remember is 2^10 = 1024.

2^12 = 2^10 * 2^2, so the answer I’m looking for is 1024 * 4 = 4096.

64^2 = 4096. Easy to do in your head when you know arithmetic (or you’ve worked too much on CPUs that don’t have MUL or DIV operators).

Project Euler

Thursday night I discovered Project Euler, a site full of math puzzles that can be solved algorithmically in under a minute each. Since then, I’ve solved the first 30-odd of them. Friday night Marcus and I sat down to solve the week’s new problem as it appeared online (we came in among the first 25 or so).

These are great fun. I think about them quite a lot as a background brain task, and I’m rediscovering how cool math problems can be. To make it a coding excercise, I’m trying too write them all in Lua and not in C, and as a result I feel that I’m getting a lot more confident in everyday Lua hacking.

Geeky pleasures.

Adventures in Optimization

This weekend I thought it would be a good idea to run the Eressea server both with and without optimizations enabled and compare the output. In theory, I thought, optimization should not change the results, and different results would hint at bugs like uninitialized variables or illegal memory access.

Needless to say, the output wasn’t the same. It was slightly different, and it looked like a small error snowballing towards the end. I’ll spare you the tale of a day trying to narrow down the exact location, and cut right to the chase:
Continue reading