Problem Definition: Euler Problem 6

This problem is very straight-forward.

“”“

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

1+ 2^{2} + … + 10^{2} = 385

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

(1 + 2 + … + 10)^{2} = 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.

”“”

if **name** == “**main**”:

Max = 100

SumOfSquare = sum([pow(X, 2) for X in xrange(1, Max + 1)])

SquareOfSum = pow(sum([X for X in xrange(1, Max + 1)]), 2)

print “%d” % (SquareOfSum – SumOfSquare)

Here is the corresponding erlang version.

-module(p6).

-export([find_diff/1]).

find_diff(Max)–>

SumOfSq = lists:foldl(fun (X, Y) –> X + Y end, 0, [X*X || X <– lists:seq(1, Max)]),

SqOfSum = math:pow(

lists:foldl(fun (X, Y) –> X + Y end, 0, [X || X <– lists:seq(1, Max)]),

2),

SqOfSum – SumOfSq.

Basically, both python and erlang offer very powerful list comprehension syntax.