<- 1:100
vec sum(vec)**2 - sum(vec**2)
[1] 25164150
Stephen Feagin
October 8, 2022
Project Euler problem 6: Sum Square Difference. The brief reads:
The sum of the squares of the first ten natural numbers is,
\[1^2 + 2^2 + ... + 10^2 = 385\]
The square of the sum of the first ten natural numbers is,
\[(1 + 2 + ... + 10)^2 = 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.
The solution for R is actually very easy to do because of R’s built-in math functions and out-of-the-box vectorization.
R provides vectorization out of the box for many functions, and it’s generally easy to apply it for functions that don’t already have it. Vectorization in this case means the ability to natively apply a function to every value in a vector, rather than operating on the entire vector as its own object. In R, this is often executed in C or C++, which makes it fast. Much faster than writing a loop and iterating over every item in the vector.
This comes into play here because I can write vec**2
to square every item in the vector, the same that I could write 4**2
to get 16. It doesn’t matter that the input is a whole vector instead of a single number, R applies it quickly to every item. At that point, it’s trivial to take the sum of the new vector.
It’s not quite as simple in Python, but still very straightforward. List comprehensions and generator expressions can do the same kind of things that vectorization do in R, and likewise very quickly.
One of the strengths of Go is that is has very little magic. That means the code has to be more verbose because you do things manually.
// For every number in the slice, add its square to a running total
func sumOfSquares(nums []int) int {
total := 0
for _, num := range nums {
total += num * num
}
return total
}
// Add every number in the slice to a running total, then
// return the square of that total.
func squareOfSums(nums []int) int {
total := 0
for _, num := range nums {
total += num
}
return total * total
}
func main() {
// Start by making the sequence of 1 to 100
seq := make([]int, 100)
for i := 0; i < 100; i++ {
seq[i] = i + 1
}
// Then take the difference between the two values
fmt.Println(squareOfSums(seq) - sumOfSquares(seq))
}
See all of my Project Euler solutions on GitHub.