Colorful programming

A few months ago, I signed up for CS 101 at  This provided a wonderful introduction to the Python programming language.  I was a pretty good Turbo Pascal programmer in high-school (early 1990s), but my programming since then had only involved short snippets in R (for statistics), SAGE (for research mathematics), and a bit of experimentation in javascript.  With Python, I finally feel like I can playfully program again.   I don’t have to worry about declaring various classes and main() things to control window objects.  Programming is fun and simple again, even if I’m only using basic functional programming constructs.

Beyond the fun, Python is useful to me in two ways. First, SAGE is wisely built upon Python, so by learning Python I can do much more in SAGE. Second, by using a Python script to output TikZ code (nothing fancy – just some string manipulations), I can create data-dense PDF graphics with mathematical precision and quality typography and color.  In other words, I have control over the graphics in my book.  Any ugliness or error is my own. An example of a graphic created with a Python script, TikZ, and LaTex, is the opening graphic for Chapter 3 found below. The Sieve of Eratosthenese is visualized by different color-filters for each prime; multiples of 2 (starting with 4) are sieved with a red line, multiples of 3 with a blue line, etc.. The white-space remaining is prime. But to see the resulting primes at the end, I switch to black-and-white, and reverse the white-space of the primes to thin black lines for the primes. This allows the reader to see the distribution of primes between 2 and 577, irregular jumps and all. To label the vertical axis, I have minimized ink by placing labels only at primes; there is no use labeling 100, 200, 300, or placing additional tick marks, when the primes themselves can be used for the same purpose.  Some fine-tuning might still be in order.

2-page spread, opening of Chapter 3
Click to enlarge the opening of Chapter 3

Now that I’ve become a Python advocate, I decided to go one step further and give Python code snippets throughout the Illustrated Theory of Numbers. You can see one in the margin on the right page. Inserting these snippets of code raises a lot of natural questions.

Why insert Python code instead of some universal pseudo-code? I am actively advocating Python, since I think the reader can benefit from learning Python, especially for future work in SAGE. Python is freely available and cross-platform. A C++ or Java programmer should be able to read Python code and translate easily enough into his or her native language.  Also, Python is surprisingly fast for an interpreted language!

Why insert code at all? Why not just make a download freely available? I plan to eventually post all code in the book, fully commented and a bit expanded (for exception-handling). This way, readers will be able to download the python script, import it, and compute to their heart’s content. But personally, I find something satisfying about personally typing short snippets of code and then clicking RUN. Maybe it reminds me of programming 20 years ago. It might be an issue of pacing. I like writing on the chalkboard as I lecture, because it sets a deliberate thoughtful pace. When I type in a code snippet myself, I think about each line and why it works. I hope readers can enjoy typing and modifying the short (5-15 lines) code snippets on their own.

Don’t existing software packages work better? The answer is certainly yes. I have not given code snippets which are perfectly optimized. I have aimed for a balance between readability and speed. Hopefully a reader can learn Python by experimenting with these snippets, and a reader with programming background can understand exactly how the snippets work. Optimizing code requires a sacrifice in clarity, or much more space for explanation. On the other hand, I have tried to give code that works well in practice, for a student of number theory. You’re not going to factor 50-digit numbers with the included code, but 10-digit numbers should factor very quickly. The included sieve code makes a list of prime numbers up to 1 million in 0.07 seconds on my MacBook Pro. This should be a good start for students, and if they want more power they can use PARI and SAGE, or maybe NZMath if they want pure Python.

A final word about the code-snippets: I still have not figured out how to properly attribute them. The Sieve of Eratosthenes snippet on the 2-page spread above is based on code posted by Robert William Hanks to StackOverflow on July 27, 2010. I will be asking the publisher for suggestions on how to make code citations in a wildly open-source world, and I appreciate any suggestions along the way.


Author: martyweissman

Associate Professor of Mathematics, UCSC. Specializing in representation theory, number theory, automorphic forms.

2 thoughts on “Colorful programming”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s