Three Lines of Code

Three Lines of Code

2017-04-29

by Richard White

Earlier this week I was walking through a school hallway when one of my students called out to me. “Mr. White! I’ve got a program that calculates primes in only three lines of code!”

I laughed, and asked him to bring it to me later in the day so that we could look at it.

“It’s just three lines!” he exulted, as if I might not have heard it the first time, “and one of those is a print statement!

Jack had taken an introductory Python course from me during the first semester of the school year, and from there had decided that he wanted to take the post-AP “Advanced Topics in Computer Science” course, a more abstract computer science curriculum that focuses on various types of data structures. Enrolling in this course requires my approval, and I had some initial concerns about how Jack might do in the course: He just had 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. In these situations, I try to err on the side of saying ‘yes.’

This semester Jack has had occasional struggles in the course, but so have most of the students, a fact that one could easily chalk that up to this being the first time I’ve taught the class. In any event, he’s making fine progress, and this prime-finder activity that he’d taken on for himself wasn’t the first time I’d seen him take something from outside the class and turn it into a piece of code.

Jack 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 comp sci 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… three lines of code!”

I thought about it for a moment.

“I bet we could get it down to one!”

“How?”

“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 would 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 open access to teachers before and after school. It might be a testimony to more open access to 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, and 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. Comic books. T-ball. Educational television. Piano lessons. Board games. AYSO soccer. Summertime concerts. Bedtime reading. Camping trips. Scissors, tape, and construction paper. Play dates. Museum visits. An allowance and an introduction to budgeting. 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 they’ll find something that inspires them.

Leave a Reply

Your email address will not be published. Required fields are marked *