Nobody Cares About a Few Million Nanoseconds

August 19th, 2011

Note: This article originally appeared on my programming book’s blog.

A Clever Programming Trick…

If you need to swap the values of two variables, this usually requires a third temporary variable (that is, if you’re not using a language like Python that supports the a, b = b, a syntax.) It looks something like this:

temp = a;
a = b;
b = temp;

But if these are integer variables, there’s a nifty trick to save yourself a little bit of memory. You can use arithmetic instead of a temporary variable:

a = a + b;
b = a - b;
a = a - b;

If the integers on your platform are 32-bits, your new swap will save four bytes of memory.

NOBODY CARES ABOUT FOUR BYTES OF MEMORY. (more…)

Vampire Numbers Visualized

July 19th, 2011

Vampire numbers are fairly interesting. Vampire numbers are numbers whose product contains the same digits as the two vampire numbers. For example, 21 x 60 = 1260. The two vampire numbers (called “fangs”) must be the same length and both cannot end with a zero. A longer example is 68088 x 45321 = 3085816248. You can find out more about these numbers on Wikipedia.

It’s fairly simple to write a program to calculate vampire numbers. I was somewhat bored and went ahead and wrote one up. Download the vampire number generator.

I’ve gone ahead and calculated all of the vampire numbers up to five digits. You can download the full text file of vampire numbers here (zipped, it’s about 1.06 MB). The nifty thing is, if you plot these numbers on an image with the two fangs being the X and Y coordinates, you get a pretty interesting image. This image is 500×500 pixels and covers the range of 0 to 100,000. Each pixel represents a 20×20 range of numbers. The more dense this area is with fangs, the darker the pixel is colored in. The 0, 0 origin is in the lower left. The final image has an interesting pattern:

Lua Cheat Sheet for Programmers

November 1st, 2010

If you don’t know Lua, the popular programming language often used as a embedded scripting language, but you do know how to program, here’s a cheat sheet that covers the basics of the language’s syntax.

The cheat sheet itself is an executable program. You can download the file here: lua_cheat_sheet.lua

For quick viewing, here it is on Pastebin

For printing, here it is as a PNG:
Lua Cheat Sheet

Attitudes on Programming for Kids: “They Need Blood Exploding Onto Their Faces Just to Keep Them From Yawning”

June 21st, 2010

I wrote a book titled “Invent Your Own Computer Games with Python” (free online under Creative Commons at http://inventwithpython.com) to teach kids (and adult beginners) how to write computer programs by making simple games. The book adopts a different teaching style from most programming books. Rather than tediously list out the programming concepts I instead present the source code to several games and introduce the concepts as I explain the code line by line. The programs themselves start off simple and text-based (“Guess the Number”, Tic Tac Toe, Hangman, etc.) and then progress in complexity.

The decision to use text-based games was done on purpose. Text is easy to present in a book and to be understood by the reader. There are no 3D models, sprites, or textures to download. By typing in the text themselves instead of using pre-made models and artwork, the reader makes the program their own rather than the product of third party content.

But from people on the Internet (who are my main audience) the most common criticism is that a programming book for kids must have fancy graphics and animation or else the kids will get bored. I receive a fair share of emails where readers and parents disagree, but we have the idea, deserved or not, that all kids live in fast-paced, ADD world of blinking lights, fiery explosions, and flashing images that we hypocritically deride and yet cater to. And if you want to teach programming, you’re going to need sophisticated toolkits with animated models, particle effects, and huge sandbox worlds that are creatable at the click of a button. I call this the “blood exploding onto their faces just to keep them from yawning” mentality. Actually typing out code is seen as dull, tedious, and an effort kids are not willing to make.
(more…)

“Invent with Python” Available in Print on Amazon

May 10th, 2010

Invent Your Own Computer Games with Python, 2nd Edition, is now available in print on Amazon.com:

Buy “Invent Your Own Computer Games with Python” on Amazon.com

Of course, the book will still be available online, in full, for free under a Creative Commons license. If you like the book, but don’t/can’t buy the print version, go to Amazon and put in a review. The book is $25 (and qualifies for Amazon’s free shipping.)

I’m very excited about this. Thanks to all the readers and people who have emailed me. Thanks!

Next Page »

Powered by WordPress