With this one being perhaps my favourite problem yet, I had to solve it twice! Feel free to download my solution and take a look, or read below to see my mathematical approach to the problem.
Euleryx Problem 6 - Sum Square Difference
My workflows and answer:
Last Weeks Favourite Solution:
After another prime number related challenge last week there was such a range of approaches. With a 3 tool solution that managed to take advantage of both the message tool, and "Cancel Running Workflow on Error" Runtime setting, our favourite solution was by @AkimasaKajitani. Having never seen the message tool used in such a practical manner, we had to cast a spotlight on this solution. Find it on page one of last weeks post, or click here.
Definitions:
On the face of it this problem doesn’t seem to be complicated; however, depending on your route to a solution, there are some key concepts we must first introduce.
Mathematical Theory:
Let's begin by taking a look at a series. As mentioned above, a series is the sum of the terms in a sequence. This can be expressed mathematically by defining the following values:
The formula then looks something like this:
Which simply reads, sum all the terms in your sequence, beginning at 0 and ending at n.
To give an example, lets look at one of the simplest cases, called the Arithmetic Series. The sequence here is simply 1, 2, 3, 4, 5….., n. There for the general term is simply expressed as = k. So the series can be expressed as:
If you were asked to calculate the sum of the first 5 terms of this sequence, you could write:
Therefore, we get the answer, 15. But what if you were asked to find the sum of the first 100 or even 1000 terms? That’s where the trick to this problem comes in. Some series have special formulas to help us find this sum quickly. Here are some useful examples:
Arithmetic series:
Quadratic series (square numbers):
But do these actually work?
Well, we can check the equation for the Arithmetic series, by using the same example as earlier.
Which is the same answer as we got before!
Whilst I won't cover the proofs in this post, if you're interested, I encourage you to explore the series further. There is a brief description provided by Project Euler upon submission of a correct answer.
Method:
Using what we have just learnt, the method for this week's problem actually becomes incredibly simple: so simple, it can pretty much be achieved in a single formula tool.
Sum of Squares:
Square of Sum:
Summary:
Whilst the brute force method here is still relatively quick, by applying some mathematical theory surrounding series, we have been able to get an answer in just one tool, and one row of data!
Want to find out more, follow this link to our introduction post - Euleryx: Let The Games Begin.
As promised on Project 1, hopefully, the one tool solution makes more sense now. If it's still a little unclear, please let me explain.
3) By taking the coefficients (numbers 3, 5 and 15) outside of the sum, this can be rewritten again, as follows: (note I used Floor(999/5) = 199 and Floor(999/15) = 66, as we only deal with whole numbers)
By looking into the problem more mathematically, I found that substruction is not necessary. More in spoiler.
@gawa - Great trick! I was thinking about doing something like that, but got scared of the maths in the binomial expansion. Your solution shows it wasn't scary at all!
Brutal Force!😁
@gawa, that's a great spot. I like how simple that expression is; it's also quite interesting how the multiple of 2 seems to just disappear, thanks to multiplication being commutative.