We are celebrating the 10-year anniversary of the Alteryx Community! Learn more and join in on the fun here.
Start Free Trial

General Discussions

Discuss any topics that are not product-specific here.

Euleryx Project 6 - Sum Square Difference

Pilsner
13 - Pulsar

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

Pilsner_15-1756992824130.png

 

 

My workflows and answer:

Spoiler
Pilsner_0-1756992608369.png

Answer: 25164150

 
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.

  • Square: A number multiplied by itself
  • Sequence: A list of numbers arranged in a specific order, typically defined by a rule or formula.
  • Series: A series is the sum of the terms in a sequence.
  • General term: (sometimes called the nth term) is a formula that allows you to compute any term in a sequence, based solely on its position. (i.e. if your sequence is even numbers, 2, 4, 6 …. The general term would 2n, therefore to find the 20th term, you would set n = 20, then calculate 2 * 20. Hence the 20th term = 40)

 

Mathematical Theory:

MathHangoverGIF.gif

 

 

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:

  • Index (term value): k
  • Ending value: n
  • General term: Pilsner_1-1756993648695.png, (think of it like a function, just like you might have y = 2x + 1 as a function, Pilsner_1-1756993648695.png = 2k + 1, is also a function) 

     

 

The formula then looks something like this:

Pilsner_2-1756992608491.png

 

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 Pilsner_2-1756993661131.png=  k. So the series can be expressed as:

 

Pilsner_3-1756992608491.png

 

If you were asked to calculate the sum of the first 5 terms of this sequence, you could write:

Pilsner_4-1756992608491.png

 

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:

Pilsner_5-1756992608492.png

 

Quadratic series (square numbers):

 

Pilsner_6-1756992608492.png

 


But do these actually work?

Well, we can check the equation for the Arithmetic series, by using the same example as earlier.

 

 

Pilsner_7-1756992608492.png

 

Which is the same answer as we got before! 

 

 

JurassicParkAlanGrantGIF.gif

 

 

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.

 

  1. Enter the value we want to sum up to. In this case, 100 (as specified by the question).
    Pilsner_9-1756992608555.png

  2. Enter the formula we have just learnt into the formula tool, I,e:

    Sum of Squares:

    Pilsner_10-1756992608556.png

     

    Square of Sum:

    Pilsner_11-1756992608557.png

     

     

    Pilsner_12-1756992608558.png





  3. Subtract the sum of squares from the squared sum to reveal your answer.

    Pilsner_13-1756992608559.png

  4. Submit your answer to the Project Euler Website!

    CompletedCompletedItMateGIF.gif

 

 

 

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.

 

10 REPLIES 10
Pilsner
13 - Pulsar

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.

Spoiler
Firstly, full credit to @gawa for explaining this at the time!

Recap: Project 1 asked us to find the sum of all multiples of 3 and 5, below 1000. 

1) As @gawa pointed out, we can apply De Morgan's Law here, which explains that we can write this problem as follows:

Sum(Multiples of 3) + Sum(Multiples of 5) - Sum (Multiples of 15) = Sum (Multiples of 3 or 5)

2) Using the Arithmetic Series discussed above, this can be rewritten as follows:

Pilsner_3-1756995691574.pngPilsner_4-1756995691574.pngPilsner_5-1756995691574.png

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)


Pilsner_6-1756995844849.pngPilsner_7-1756995844849.pngPilsner_8-1756995844849.png

4) Finally, implementing the Arithmetic Series formula we have just learnt, we get the following:

(3 * ((1/2) * 333 * (333 + 1))) + (5 * ((1/2) * 199 * (199 + 1))) - (15 * ((1/2) * 66 * (66 + 1)))

 


 

 

Pilsner_9-1756996012301.png

 

AkimasaKajitani
17 - Castor
17 - Castor

Team brute force!

 

Spoiler
AkimasaKajitani_0-1756997769934.png

 

Thank you for picking up my last week solution! @Pilsner 

gawa
16 - Nebula
16 - Nebula

By looking into the problem more mathematically, I found that substruction is not necessary.  More in spoiler.

Spoiler
According to simple binomial theorem,
(a1+a2+...+an-1+an)^2 = (a1^2+a2^2+...+an-1^2+an^2)+(a1*a2+a1*a3+...an-1*an)
Hence
(a1+a2+...+an-1+an)^2 - (a1^2+a2^2+...+an-1^2+an^2)= Σai*ak (1<=i,k<=n, where i≠k)

For example,
(a+b+c)^2=(a^2+b^2+c^2)+2ab+2ac+2bc
Hence
(a+b+c)^2-(a^2+b^2+c^2)=2ab+2ac+2bc

 

Carolyn
12 - Quasar
12 - Quasar

@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!

Carolyn
12 - Quasar
12 - Quasar

Team Brute Force!

 

Spoiler
2025-09-05 10_15_11-Alteryx Designer x64 - Euler_006_Carolyn.yxmd.png

 

DaisukeTsuchiya
14 - Magnetar
14 - Magnetar

Team brute force!

 

Spoiler
スクリーンショット 2025-09-09 110544.jpg

 

Qiu
21 - Polaris
21 - Polaris

Brutal Force!😁

Spoiler
Euleryx Project 6.png
Yoshiro_Fujimori
15 - Aurora
15 - Aurora

I learned many ways to get the formula of "sum of square" on the net.

 

Spoiler
Workflow
Euleryx_6_Workflow.png

 

 

Pilsner
13 - Pulsar

@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. 

Labels
Top Solution Authors