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.

## Discussion (34)

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:

my code with C++

/*Sum square difference

Problem 6

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

12+22+...+102=385

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

(1+2+...+10)2=552=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.*/

## include

using namespace std;

int square_sum(int s);

int main()

{

int limit=100 ,sum=0 ,sumQ=0;

for(int i=1;i<=limit;i++)

{

sumQ=sumQ+(i*i);

sum =sum+i;

}

}

int square_sum(int s)

{

return s*s;

}

Output >> 25164150

not bad)))

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

public class ProjectEuler{

static int n = 100;

public static void main(String []args){

System.out.println(differencehelper());

}

Easy Java Solution using Gauss' Formula Hope you liked it ;)

Ruby 2.7

Swift:

new_list = []

for i in range (1,101):

new_list.append(i*

2)*2a = sum(new_list)

b = sum(range(1,101))

print(b-a)

I like your posts!

new_list = []

for i in range (1,101):

new_list.append(i^2)

a = sum(new_list)

b = sum(range(1,101))^2

print(b-a)

Elixir: