# Project Euler #6 - Sum Square Difference

### Peter Kim Frank γ»1 min read

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 May 29

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 !

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

Ruby:

Ruby is so elegant π

My take on this problem in python:

Output:

Rust Solution: Playground

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.

Some fun!

## Benchmark result:

Rust

Swift:

Ruby 2.7

Elixir:

I like your posts!