The first thing I wanted to do was write a routine to generate a triangle number. As we've seen throughout this series, LINQ can come in very handy:
Now I needed to find all the divisors of a number. I have to "eat crow" and admit the first time I did this, I used the brute force method:
I didn't like it. I knew I could use the square root to reduce the number of iterations in this loop, but that would mean every "select" of my LINQ query would need to return more than one result -- the divisor and the number / divisor. I couldn't see any easy way to create two results from a query so I just left this as-is and moved on to actually solving the problem.
Again, LINQ made this extremely easy:
As you might expect, this wasn't very quick. In fact, I let it run overnight since I didn't start on this till later in the evening. It ended up taking 5 hours and 15 minutes. NOT A SOLUTION!
So I started researching how I could return multiple items from a single LINQ query. The answer was embarassingly simple -- create an array!
But now I've got an IEnumeable<int> and I want all the divisors together in an IEnumerable<int>. I looked around at the extension methods on IEnumerable<T> and found SelectMany. This method will project each element into an IEnumerable<T> and flatten the result into a single sequence.
So my revised FindAllDivisors now looks like this:
Now this completes in a respectable 5 - 6 seconds. MUCH better than my first attempt. Sometimes, KISS only gets you so far.