2 quick lessons from a C# coding exercise
This simple problem reminded me not to overthink — unless you have to.
I still like doing coding exercises, even after years of programming. I recently came across this neat exercise — a simplified Pythagorean triplet — that reminded me not to overthink problems until you really have to.
Fair warning — this is not a hard problem. But I want to remember these takeaways:
(1) You don’t always need to do a lot of work to figure out a good complexity algorithm for a problem. Sometimes we want to grab our favorite data structures right away, but sometimes also reworking the problem can cut down the complexity.
(2) A good guess is often good enough — e.g. working out better ranges for loops is sometimes not even worth it, and even a simple guess will suffice until we really have to solve it.
The problem
The Pythagorean triple is three positive integers a,b,c
that satisfy:
A common coding exercise is to efficiently find all a,b,c
that are each less than N
that satisfy this equation (emphasis on efficiently).
A simplified version of this problem is to consider another condition:
This addition greatly simplifies the problem, and a very efficient algorithm can be easily…