Five years ago, I took a perl class, and I'm still angry about it.
This was the last class I took in an official, letter-graded, collegiate context. I had finished my academic career three years prior. I signed up mainly so that a dear friend of mine wouldn't have to take the class alone. And I just want to say (in case she is reading this) none of this is her fault. I had every intention of doing it with good grace. It crossed my mind that I might learn something, or at least I might be able to construct an educated basis for my general distain for perl.
Around week 7, the instructor (to whom I will refer as Steve) introduced an extra credit programming contest where we could presumably win prizes by solving a challenge problem about something called a sum-product number.
I'm now going to explain the challenge problem, but you should understand: this is a trap. Don't try to do this problem. Remeber that scene in Portal 2 where GladOS thinks she can defeat Wheatley by giving him a paradox, but it doesn't work because Wheatley is too thick to appreciate it? Well, this isn't like that, because you are not Wheatley. You're intelligent and self-aware and care about math and computer science or you wouldn't have read this far. You'll get sucked in to this, and you won't be able to get out. I'm warning you.
A sum-product number (in base-10) is an integer that is equal to the sum of its digits times the product of its digits. 135 is a sum-product number because:
135 = (1 + 3 + 5) × (1 × 3 × 5)
There are only four sum-product numbers in base-10, and they are:
0, 1, 135, and 144
The proof that these are the only four (attributed to David Wilson) is computer-assisted. A formal argument can be made that there are no sum-product numbers past a certain point. A computer program verifies that up to that point, there are only the four listed above.
Computer-assisted proofs are cool, and programming contests are also cool, but they don't mix very well. Our assignment for the contest was to write a perl script which (as efficiently as possible) printed all the sum-product numbers from 1 to 10,000,000, one to a line, each with the text: " is a sum-product number!!"
This is the code I wrote to get started:
#!/usr/local/bin/perl
use integer;
my ($i, $t, $s, $p, $d);
for($i = 1; $i < 10000000; $i++) {
$t = $i;
$s = 0;
$p = 1;
do {
$d = $t % 10;
$p *= $d;
($d != 0) or next;
$s += $d;
} while($t/=10);
if( ($p != 0) and $s * $p == $i ) {
print $i, " is a sum-product number!!\n";
}
}
Here's the issue. Normal programming contests ask for a program that takes input and computes something which depends on that input. A submitted program gets judged by running it with input or inputs which are kept secret. Because the input is kept secret from you, you must write a correct algorithm. Your logic cannot be wrong or you would risk getting the wrong answer and therefore losing the contest.
But the challenge from this class didn't take an input; it simply asked for the result of one particular query, so it just had one correct output, this:
1 is a sum-product number!!
135 is a sum-product number!!
144 is a sum-product number!!
Presumably, the fastest-executing entry wins, so one is tempted at first to enter this script:
#!/usr/local/bin/perl
print <<'X';
1 is a sum-product number!!
135 is a sum-product number!!
144 is a sum-product number!!
X
...which prints the correct output without doing all that pesky, time-comsuming computation.
Obviously this is not how the contest is intended to be won, but why not? I guess because it goes against the spirit of the problem which is to replicate the work of the original computer-assisted proof.
So then, it seems like for an optimization to be valid, it would have to be accompanied by an argument that the work saved was non-essential to the computer-assited proof. For instance, if you can argue that none of the numbers in some large range have the sum-product property, then you can rewrite the loop to skip over that range. But what if your argument is wrong? Then you cull away a bunch of work based on your wrong logic, get the right answer and win the contest? That's not fair.
Thinking along these lines led me into a torpor. I had already gotten kinda sick of the class.
Today, I dug through some old emails and found the rant I wrote to the other students in my study group. Here it is, exactly as I wrote it except with the instructor's name replaced.
I pissed away the whole day ruminating about perl and struggling with the profound powerlessness I feel in the face of the pedagogical catastrophe that is this class. This class has a way of sending me into a maelstrom of second-guessing and self-doubt. I try to work on the homework, but I wind up pacing and obsessing, pouring over details and swishing thoughts around in my mind, trying to understand perl, or worse, trying to understand Steve's motivation. It starts out logical, but it winds up a cognitive slurry, and by the end, I'm sulking in the tub, pouring back over the day, trying to figure out when the transition to madness occurred. Have you looked at the extra credit assignment? May the devil take this goddamn piece of shit. If you do look, I promise, you'll discover something about yourself. You'll think you're solving a puzzle, but you're not, what you're doing is allowing the inception of thought-poison. Soon you'll be looking for loopholes in the rules, and wondering if the solution you've come up with is cheating or not. You'll contemplate doing it anyway, just to have a fighting chance against the idiot who didn't even think about whether it was cheating. That's not fun! Coding contests are supposed to be fun! They are, in fact, one of my favorite things. I love the art of creating cunning, corner-cutting code that does something brilliant from within a constrained framework. It's the white wine of computer engineering distilled to make cognac. It's the thrill of moving the world with Archimedes' lever. But instead of relishing the contest, I'm playing prisoner's dilemma and smudging my personal ethics. Up until now, I've found Steve's bad pedagogy tolerable. He designs each homework around calendar-related application instead of selecting an application that's well suited to the current topic. In my opinion, he alienates people on the discussion forum by answering every question he can by reference. He devotes an inappropriate portion of the text he writes to warning the students about how challenging the class is. It's defensive and wastes time. But this... this is psychopathy. He's taken away Christmas.
I got an A in the class.