"I just upvoted your old post. :-)"
Thank you!
"many who see the code would get confused"
I honestly don't think we should dispel confusion by dumbing things down to the point where all discrete math is squeezed out of the CS curriculum and all you have left is lame switch-cases and explicit enumeration so there is zero confusion but also zero insight into the problem. Some of the candidates we interview these days are a symptom of this very problem. Lemme give you 2 examples ( actual interviews questions we asked candidates ).
1. write a function that returns the sum of the first one thousand odd numbers.
Expected Answer:
function foo() {
return 1000*1000;
}
The candidate's answer :
function foo() {
sum = 0;
count = 0;
i=0;
while(true) {
i++;
if (i%2==1) {
count++;
sum += i;
}
if( count == 1000 ) {
return sum;
}
}
}
Now, the candidate is a LAMP expert and can do magic with linux sys-admin tools, but really, this is such an incredibly lame solution. Yes, its "correct" and very clear and nobody will get confused looking at his solution. But what's more clear is that this person has never had a single course in discrete math. Sum of n odd numbers is n^2 is taught on day 1, its so fundamental and basic and even if you forget the actual result, it takes just 5 minutes to derive it from first principles ( just write the sum forwards to backwards, then write it again backwards to forwards, then add the two expressions, hey voila there you go! ). When we show him the expected solution, he is genuinely surprised and says we've pulled a fast one on him. "Its a trick question! ". He says he's never been taught anything like this in the classroom. That's just sad.
2. Find all the integer triples that form right triangles whose smallest side is less than a million.
Candidate's answer :
for i from 1 to 1000000 {
for j from 1 to 1000000 {
for k from 1 to 2000000 {
if i*i + j*j == k*k then print i,j,k;
}
}
}
Again, this is the "correct" solution and its eminently readable, but it is supremely inefficient and super-lame. Its like the candidate has never given any thought to the actual problem, and is just throwing loops and conditionals at it. I will refrain from giving the expected solution here, but any student of discrete math can generate pythagorean triplets in a much more efficient manner than the lame solution given above.
A little dose of Knuth's Concrete Math goes a very long way.