Three lines of code by Richard White, Upper School science teacher

I was walking down the hall at school when one of my students called out to me. “Mr. White! I’ve got a program that calculates prime numbers in only three lines of code!” I laughed and asked him to bring it by later in the day to show me. “It’s just three lines!” he exulted, as if I might not have heard the first time, “and one of those is a print statement!”

The student had taken an introductory computer science course from me during the first semester and from there had decided to sign up for an Advanced Topics course with a more abstract and challenging curriculum. Enrolling in this course requires instructor approval, and I had some initial concerns about how he might do in the course — he had just a few months of experience, and the curriculum would be a significant step up for him. While I didn’t want to set him up for failure, I didn’t want to dampen his enthusiasm for the subject either. I decided to approve his request to take the course.

During that second semester, the student had occasional struggles in the advanced course, but so did most of the other students — a fact that one could easily attribute to this being the first time I’d taught the class. Ultimately, he made fine progress, due in part to his interest in combining his other interests with computer science. This extracurricular prime-finder activity that he’d chosen to tackle wasn’t the first time I’d seen him turn something he′d found outside of class into a piece of code.

The student tracked me down again after school, laptop already open, ready to show his code. And sure enough, there it was, a tiny little Python program of just three lines. And it worked!

“What kind of algorithm is this?” I asked. It wasn’t the standard introductory computer science treatment of primes I used in my teaching. “This is ‘Wilson’s Theorem,’” he explained. “I learned about it on YouTube. It’s not very good for large prime numbers because it requires calculating factorials, but still … just three lines of code!”

I thought about it for a moment. “I bet we could get it down to one!”

“How?” he asked.

“Do you remember Python’s ‘list comprehension’ syntax? I bet we can use that to make this a one-line program that calculates primes!” And so we did:

Last login: Sat Apr 29 09:05:01 on ttys000

rwhite@MotteRouge$ python

Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:52:12)

[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import math

>>> [x for x in range(2,1001) if (math.factorial(x-1) + 1) % x == 0]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]


Just about every teacher I know will have his or her own lesson to be learned from this anecdote. It could point to the need for more free time in school so that students can find their passion. It could be a call for more access to teachers before and after school. It might be a testimony to opening up advanced classes or an encouragement to allow more students to take academic risks. A math teacher might read it as a need for us to teach more computer science in our math classes; a computer science teacher might claim that this demonstrates the utility of Python as an educational programming language.

There’s some truth in all of those, I think. Every educator would like to find more ways to empower our students to find and follow the interests that inspire them. Some students find their passion more quickly or easily then others, but every parent knows the secret to helping kids flourish and grow: Throw stuff at them and see what sticks.

Bedtime reading. Educational television. T-ball. Piano lessons. Play dates. Comic books. Board games. AYSO soccer. Summertime concerts. Camping trips. Scissors, tape, and construction paper. Museum visits. An allowance and an introduction to budgeting. A Leatherman multi-tool. An electric guitar. A woodworking class. Batteries and wire. Dance lessons. Give students time, space, exposure to new ideas, and the tools to build on those ideas, and their curiosity will take care of the rest. They’ll find something that inspires them.

The desire to help students along this path is a core tenet of the POLYSolves committee. We′re looking for ways the school can create conditions in which students can develop enthusiasm for an idea, a subject, or a project. And we′re interested in how we can create better opportunities for students to pursue those ideas, to study those subjects, and to build those projects. Inspiration may come from a classroom discussion, a math problem, a service project … or even a YouTube video. Regardless of the source, the goal is the same: supporting students as they pursue their passions.