# Project Euler #6 - Sum Square Difference

### Peter Kim Frank ・1 min read

Project Euler (7 Part Series)

1) Project Euler #1 - Multiples of 3 and 5
2) Project Euler #2 - Even Fibonacci numbers
3 ... 5
3) Project Euler #3 - Largest Prime Factor
4) Project Euler #4 - Largest Palindrome Product
5) Project Euler #5 - Finding the Smallest Multiple
6) Project Euler #6 - Sum Square Difference
7) Project Euler #7 - 10001st prime

Continuing the wonderful community solutions to Project Euler.

This is Problem 6, sum square difference.

The sum of the squares of the first ten natural numbers is,

1² + 2² + ... + 10² = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)² = 55² = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Classic DEV Post from Dec 25 '18

no loops!

python3

`def sum_square_difference(n):`

`return (((n**2) * (n + 1)**2) / 4) - (n * (n + 1) * (2*n + 1) / 6)`

`print(sum_square_difference(100))`

I dont know why when I tried to upload images they dont appear

Aw yeah 👌

If some are wondering, that comes from well-known formulas for the sum of the first

nintegers andnsquares. There's a generalization, too, but also a pretty common formula for the firstncubes.A solid mathematical approach can really simplify programming challenges. It almost feels like cheating.

Definitely highlights the need for maths in software development.

Woah, alright, you win 😲.

Also, there's a guide here to embed code and images in markdown (what dev.to uses).

thanks, I'm going to check it.

Python!

I surrender!

Javascript !

Yours is good. But I have a feeling it uses more then one for/for each loop. May explain why it is a bit slower then mine. Not sure.

Yeah, also the fact that I'm iterating 2 times the same array !

Ruby:

Ruby is so elegant 😍

Here is my nodejs solution

output

Yours performs faster than the one I submitted, and still looks nice, well done !

EDIT: some of the

`let`

s could be replaced with`const`

though 🙈true

Rust Solution: Playground

My take on this problem in python:

Output:

Rust

Some fun!

## Benchmark result:

From ProjectEuler:

Please do not deprive others of going through the same process by publishing your solution outside of Project Euler; for example, on other websites, forums, blogs, or any public repositories (e.g. GitHub), et cetera. Members found to be spoiling problems will have their accounts locked.

:)

## Go

## goplay.space/#rQ0XekiCknZ

You can see the complete program on the playground link.

Ruby 2.7

Swift:

I like your posts!

Elixir: