How do I start Programming?

These excerpts are from a response to a reader on the wonderful tumblr by Jerome Herr, Experiments In Processing.

Learning to code is like learning a natural language: it’s quite hard and slow at first but if you hold on just a little then suddenly you start seeing all the beauty of it and you’re amazed about how you suddenly understand what’s going on and how you can create stuff (designs or sentences) on your own!

Start with something that you understand and then, VERY IMPORTANT, play with it! What I often do is that I simply add a new instruction, maybe one from a previous tutorial or sketch, to see how that will look. Or I wonder: what will the animation look like if I use very big ellipses? or very small ones? only 10 of them? or a 1000? What if I make this rectangles rotate a bit? A lot? What if I apply this to only some of them?

So it’s not just about staring at the screen. It’s also about sketching stuff on paper and about letting yourself be inspired by everything you see in the world around you! Look at all the patterns that you can see in the natural world, day in and day out: in buildings, on the sidewalks, in the behaviour of cars or birds. Try to recreate those patterns. Start with the easy ones, obviously ;) Look at Muslim art, lots of patterns to be found there. Or the art of Sol Levitt. And go to art museums, they can be very inspiring anyway!

Car Talk Product Placement Puzzler


RAY: There are a lot of our little children who listen to Car Talk who are studying Algebra, so I thought I would ask them to simplify the following polynomial.

TOM: Let me just write this down. OK.

RAY: X plus A in parentheses times X minus B in parentheses times X plus C in parentheses, dot, dot, dot, times X minus Z.

Here’s how it looks: (X+A)(X-B)(X+C) … (X-Z)

TOM: So you want —

RAY: I want the product of all those terms.

Car Talk Annulus Puzzler


RAY: This puzzler is from a fellow named Jerry Olsen. Ed and his two sons, Biff and Skip, have been hired to paint the floor of a merry-go-round. They want to make sure they measure the floor area exactly, because they don’t want to buy any extra paint. The carousel, of course, is a circle. Here’s the catch: In the middle of the carousel is a smaller circle, which contains all the machinery for the carousel.

TOM: An annulus, in other words.

RAY: Exactly. It’s a ring we’re dealing with. Ed tells Biff, “We need to know the area of the carousel, including the area of the big outer circle that we’re going to paint and the area of the inner circle where there’s nothing but the machinery. “Once we have the areas of both circles we can subtract the inner circle from the other circle and we’ll know how much paint we need.”

Biff goes to the carousel and says to himself, “I can’t do this. All the machinery is in the middle. I can’t get to the center to measure the diameter.” He thinks, “I’ll cheat. The old man will never know!” Biff measures a straight line from one edge of the carousel to the other edge, not going through the center.

TOM: In other words he’s going to make what’s called a chord of the big circle.

RAY: Right. Any line that goes from one edge of the circle to the other that isn’t a diameter is a chord. As luck would have it, the tape measure touches the inner circle, or in geometric terms, is tangent to the inner circle at one point.

Biff returns to his dad and says, “I couldn’t do what you wanted me to do. I got this measurement and it’s 70 feet.” The old man administers a swift dope slap. He says, “How the heck are we going to figure this out. We don’t know either diameter.”

The other brother Skip says “I think I can figure it out. ”

Can he or can’t he?

Skype with Dark Sky

(This is a crosspost from my weekly photo site.)
The Introduction to Computer Programming Class had a Skype Q and A with Jason from Dark Sky (Dark Sky is an iOS weather app, check it out, it’s fantastic. They also make the great weather site Super nice guy, I just emailed and asked, and it was scheduled a day later. Great experience for these kids.

Here’s some student responses to the prompt: “What blew your socks off? What’d advice/stories/information was surprising?

  • When how he told that if you really want to learn something. you need to be able to do it on your own time
  • I think it is motivational that someone who is successful had a hard time and still does sometimes and still does what he wants to do.
  • I thought it was really interesting when he talked about “reverse engineering” video games, and that was how he learned trigonometry. But now that I think about it, it isn’t surprising that it was easier for him to learn something difficult while immersing in something he was passionate about.
  • The most surprising fact was that he wrote 45,000 lines of code to make the app originally, and then he modified it to do more, but only required 8,000 lines of code. I also really liked how he encouraged people to go on their own and explore other programs by themselves.
  • What I guess what surprised me the most was how he compared computer programming to dance or singing or art. Going into this class I perceived computer programming as a very technical and systematic subject…that everything is by the book. While this may be true… talking to the developer brought to my attention that computer programming can be largely reliant on self discovery and self error.
  • I was shocked when Jay told us how long it took to create his app and how many lines of code it required (45,000).

IMAG0262 IMG_4075

Kaprekar’s constant

A student talked about Kaprekar’s constant (6174) during their my favorite presentation.
Really cool.

Steps (from wikipedia):

  1. Take any four-digit number, using at least two different digits. (Leading zeros are allowed.)
  2. Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary.
  3. Subtract the smaller number from the bigger number.
  4. Go back to step 2.

Here’s an example (also from wikipedia):

For example, choose 3524:

5432 – 2345 = 3087
8730 – 0378 = 8352
8532 – 2358 = 6174

Fun right? Also fun to program, here’s the python code that tries every number from 1000 to 10000, and counts how many steps it takes to get to 6174 and puts the results in a csv file:

def largest(nstr):
    if (len(nstr) == 0):
        return ""
    elif (len(nstr)==4) and (nstr[0] == nstr[1] == nstr[2] == nstr[3]):
        return "7641"
    digit = -1
    index = 0
    for i in range(0,len(nstr)):
        if (digit < int(nstr[i])):
            digit = int(nstr[i])
            index = i
    return str(digit) + largest(nstr[0:index]+nstr[index+1:len(nstr)])
def kaprekarSteps(n):
    count = 0
    nstr = str(n)
    while (n != 6174):
        l = int(largest(str(nstr)))
        lstring = str(l)
        s = int(lstring[::-1])
        n = l - s
        nstr = str(n)
        while (len(nstr) < 4):
            nstr = "0" + nstr
        count += 1
    return count
f = open('kaprekar.csv','w')
c = 1000
while (c < 10000):
    f.write(str(c)+ "," + str(kaprekarSteps(c)) + "\n")
    c += 1


Euclid GCD

I saw this toot by Matt yesterday morning and I loved the visual of the color based on the number of steps of Euclid’s GCD algorithm. The algorithm is pretty straightforward and it’s a nice example for either using recursion or using a loop. The coloring is fun to mess with too. The coding went quickly for me because I already had the code for breaking a 1D pixel array (why processing??) into x and y coordinates.
There’s two variations:
Here’s the link for the GCD steps version.
And here’s the link for the GCD version where the closer the GCD is to the minimum of x and y, the more white the pixel is.
Here’s the recursive algorithm:

int euclidGCD(int x, int y)
  if (y == 0)
    return x;
  else if (x >= y && y > 0)
    return euclidGCD(y,(x%y));
    return euclidGCD(y,x);

Here’s the loop algorithm to count the number of steps:

int euclidGCDsteps(int x, int y)
  int t;
  int steps = 0;
  if (x >= y)
    while (y != 0)
      t = y;
      y = x % y;
      x += t;
    return euclidGCDsteps(y,x);
  return steps;