I am not at all sure how far I will get with this, but I am going to try. So far I have solved the first 7 problems from Project Euler, using PowerShell. I will, whenever I solve a problem, take my time to write about my solution and what I’ve learned about math and PowerShell. It should be a good read for anyone not particularly a wizard at math, because I don’t have a math major either. Everything I write here should be in reasonably understandable terms, because I need to understand it too!
Either way, let’s start with the first problem, defined as below.
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
This is easily solvable using a simple formula, but first, a naïve script to calculate this:
(1..999)
| where {$_ % 3 -eq 0 -or $_ % 5 -eq 0}
| measure -sum
| select Sum
So basically, what I do here is create an array ranging from 1 to 999, select all items which are divisible by 3 or 5, and add them together. Then, for good measure, I select the ‘Sum’ property of the measure info object.
What I learned here is basically how to create a range (similar to Enumerable.Range in C#), and how to measure a variable in PowerShell.
This is not the optimal solution. This problem is actually best solved simply by using a simple formula for calculating the sum of a range of numbers:
$$\sum_{i=1}^n i = {n^2 + n \over 2}$$
Basically, for any range of numbers with a regular interval you can find the sum with this formula. Something even more interesting is that it is possible to also find the sum for a sequence of $i^2$, $i^3$, and so on. This will be useful for one of the next problems, so I will elaborate on that when I get there.
It’s easy to prove how this formula works. First, we take the sequence and put the reverse sequence below it, just for visualization. For the example I’ve taken {1,2,3,4,5}, it’s a bit shorter:
1 2 3 4 5
5 4 3 2 1
What’s so special about this? Well, every pair of the top and bottom row is equal to exactly 6, or (n+1). How many pairs are there? Well, that’s easy. In this case, 5, but in a general case, we could say there are ‘n’ pairs. So the sum of these pairs is equal to: $(n+1)n$ which can be reduced to $n^2 + n$. However, because we’ve duplicated the sequence, our sum is now twice the amount we’re looking for. Simply divide by two (= remove the duplicate sequence) and voila: we have our formula!
However, we’re not looking for {1,2,3,..,n}. We’re looking for all terms divisible by 3 or 5, or {3,6,9,..,n} and {5,10,15,..,n}. Luckily, we can just multiply the sequence for {1,2,3,..n} by 3 and 5 to get those. We just need to find the upper bound for both, to make sure the $3n$ and $5n$ won’t be greater than 1000. In other words, less than or equal to 999. It’s not much trouble calculating this:
999/5 = 199.8. Round down to 199.
999/3 = 333. No rounding needed.
But wait! we also need to make sure we’re not adding values twice. Every time a value is divisible by 3 and 5, we’ll add it twice if we simply add the two sequences. So we’ll need to make sure that the values divisible by 15 (= 3 * 5) are removed from the sequence once. Again, let’s find the upper limit:
999 / 15 = 66.6, round down to 66.
Adding all these values gives us the following equations:
$$\sum_{i=1}^{333} 3i + \sum_{i=1}^{199} 5i - \sum_{i=1}^{66} 15i = 3{{333^2 + 333} \over 2} + 5{{199^2 + 199} \over 2} - 15{{66^2 + 66} \over 2}$$
Problem solved, with and without programming! As for the title, I’ll leave it as an exercise for you as a reader to look up the story about little Carl Friedrich Gauss, and how it relates to this problem and it’s solution.