h4ck3r.net

Euler Project in F# Problem 6

15 May 2008

Kick it, Euler:

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.

Not much to this one. I learned about the exponentiation operator which seems to be a rewrite to calling a Pow method (it doesn’t seem to work on int). Here’s a “my brain is currently warped into thinking about everything as a sequence and operations on sequences”-solution:

([1.0..100.0] |> Seq.fold (+) 0.0) ** 2.0
- ([1.0..100.0] |> Seq.map(fun x -> x*x) |> Seq.fold(+) 0.0)

See y’all next time.