#6: Sum square difference | Ben Cunningham

#6: Sum square difference

Problem by Project Euler · on December 14, 2001

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

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

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.

Python

m = 100
ssq = sum([n ** 2 for n in range(1, m + 1)])
sqs = sum([n for n in range(1, m + 1)]) ** 2

ans = sqs - ssq

print(ans)
## 25164150

R

m <- 100
ssq <- sum((1:m)^2)
sqs <- sum(1:m)^2

sqs - ssq
## [1] 25164150