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.

## Top comments (31)

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!

Ruby:

Ruby is so elegant 😍

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

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.

:)

Rust Solution: Playground

My take on this problem in python:

Output:

Here is my Javascript Solution

Rust

## Go

## goplay.space/#rQ0XekiCknZ

You can see the complete program on the playground link.

Some fun!

## Benchmark result:

Swift:

Ruby 2.7

hi

i try this:

sum of numbers= n(n+1)/2

then i get the square.

after that i found an equation about sum of squares:

(2*n^3 +3*n^2+n )/6

but i didn't get right result !!!!

so where i lost the control ?

thanks