DEV Community

Discussion on: Project Euler #6 - Sum Square Difference

Collapse
 
lmbarr profile image
Luis Miguel • Edited

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

Collapse
 
maxart2501 profile image
Massimo Artizzu

Aw yeah 👌

If some are wondering, that comes from well-known formulas for the sum of the first n integers and n squares. There's a generalization, too, but also a pretty common formula for the first n cubes.

A solid mathematical approach can really simplify programming challenges. It almost feels like cheating.

Collapse
 
natonathan profile image
Nathan Tamez

Definitely highlights the need for maths in software development.

Collapse
 
badrecordlength profile image
Henry 👨‍💻

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

Collapse
 
lmbarr profile image
Luis Miguel

thanks, I'm going to check it.