I was translating Pascal to C as usual for Missions when I came across the code fragment for computing the time bonus earned on victory given the current game time:

```
1
2
``` | x := BSR(gCycle, 4) + 1; x := round(50000 / x) * 10; |

Now, I could have translated that to C as `timeBonus = (50000 / ((cycle >> 4) + 1)) * 10`

, but I decided to get clever. Uh oh. I applied algebra to simplify the equation.

To translate it algebraically, I had to convert it to a purely mathematical equation. The right shift operator `>>`

isn’t algebraic, but given the identity that `x >> y = x / 2`

, a right shift of four bits is an integer divide by 16. “cycle” is just a variable, so call it x, giving us the algebraic equation ^{y}`(50000 / ((x / 16) + 1)) * 10`

, or:

Next, multiply 10 (as `10 / 1`

) into the equation:

Using the equality `a/b + c/d = ad + bc / bd`

, rewrite `(x / 16) + 1`

:

Rewrite using the equality `a / (b / c) = a * (c / b)`

:

Leaving us with two operations (add and divide) where originally there were four (shift, add, divide, multiply). Isn’t math cool?

All equation images generated by Apple’s Grapher program. Nice little feature that thing has, copy equation as TIFF.

Rubes…which just goes to show that I have never fully embraced math!

Gwynne RaskindPost authorI’m convinced for some reason that I still haven’t reduced it to its most minimal form, but I can’t seem to work out any way to rewrite it in any less complicated way than shown above. Though I do remember enough of my calculus to say that derivative

`f'(x)`

would represent the rate at which the time bonus changed relative to the time expended, with which I could calculate the minimum and maximum time bonuses (`lim f(x) as x->Â±âˆž`

), and other fun but completely useless facts.