Project Euler problem #6 is
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
When I first began looking at this problem I wanted to experiment a bit with the Parallel Extensions for .NET, so I started where any parallel algorithm begins: with a sequential algorithm. The algorithm here is rather trivial: Loop over the natural numbers from 1 to 100. In order to sum the squares I will sum the square of each number. In order to square the sum I will sum the numbers and then square them. The difference is the answer. So, first, I came up with two functions: SumOfSquares and SquareOfSums.
My first attempt at these functions ended up something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | static double SumOfSquares(int min, int max) { double result = 0; for(int x = min; x <= max; x++) { result += Math.Pow(x, 2); } return result; } static double SquareOfSums(int min, int max) { double result = 0; for(int x = min; x <= max; x++) { result += x; } return Math.Pow(result, 2); } |
Both of those functions are very straight forward. My first thought after writing these was, "Hey, why not use some LINQ?" So I did. Here is how the functions change:
1 2 3 4 5 6 | static double LinqSumOfSquares(int min, int max) { return Enumerable.Range(min, max).Select(d => Math.Pow(d, 2)).Sum(); } static double LinqSquareOfSums(int min, int max) { return Math.Pow(Enumerable.Range(min, max).Select(d => d).Sum(), 2); } |
Wow! Talk about incredible, shrinking functions! I love finding a way to make code more succinct, readable, and elegant and these changes seem to have hit the nail on the head! Anyways, that is basically all the pieces for the sequential algorithm. All you have to do is call each of those functions and take the difference. Simple, huh? But the real question is how can we parallelize these bad boys? I have a few thoughts.
- Call each function in parallel. In other words let the sumOfSquares and SquareOfSum function run at the same time in different threads.
- Parallelize each function invidvidually. In other words leverage the loops inside of each function in order to parallelize them.
So let's take a look at each method using the Parallel Extensions for .NET. The first method is calling each function in parallel. My first thought here was to use Parallel.Invoke in order to call each function at the same time. A little further research quickly revealed that Parallel.Invoke cannot return any values. My initial response to that: "Well that sucks." Luckily there's another class in the Parallel library called Futures. What's a future? From DevX -
"In TPL terms, a Future is basically a task that returns a value. It's like a deferred function. You start it running and then use its value later. If the Future hasn't finished calculating its value by the time you need it, it makes you wait while it finishes."
Sounds good to me. So how do we use futures? Like this:
1 2 3 4 5 6 7 8 | static long Problem6Futures(int min, int max) { Future<double> fSumOfSquares = Future.Create(() => SumOfSquares(min, max)); Future<double> fSquareOfSums = Future.Create(() => SquareOfSums(min, max)); double result = fSquareOfSums.Value - fSumOfSquares.Value; return result; } |
Easy, huh? All you have to remember is that Futures are deferred functions that return values. The result of Future operation gets stored in an object of type Future
1 2 3 4 5 6 | static double ParallelSumOfSquares(int min, int max) { return Enumerable.Range(min, max).AsParallel().Select(d => Math.Pow(d, 2)).Sum(); } static double ParallelSquareOfSums(int min, int max) { return Math.Pow(Enumerable.Range(min, max).AsParallel().Sum(), 2); } |
Hah! Even easier! All that I did was add .AsParrallel() to our data. That tells LINQ to do the processing in parallel!
So, there's Project Euler Problem #6 for you. Was it a hard problem? Not really. Are you going to see major performance results through the parallelization of this algorithm? No. But you gotta' start somewhere when you're learning how to parallelize algorithms using a new library. Today I used Futures and PLINQ and that sound's like a pretty solid start to me.