Problem 6 was surprisingly straight-forward.
“Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.”
Really, just a straight-forward calculation, easily implemented in Erlang recursive-speak. No real comments on this one.
-module(euler6).
-export([difference/1]).
difference(N) ->
SumSquare = sumofsquares(0,N),
Sum = squareofsum(0,N),
SquareSum = Sum * Sum,
io:format("Square ~w Sum ~w ~n", [SquareSum, SumSquare]),
Difference = SquareSum - SumSquare,
Difference.
sumofsquares(Sum,N) ->
if N >= 1 ->
NewSum = (N*N) + sumofsquares(Sum,N-1);
true ->
NewSum = Sum
end,
NewSum.
squareofsum(Sum, N) ->
if N >= 1 ->
NewSum = N + squareofsum(Sum, N-1);
true ->
NewSum = 0
end,
NewSum.

Post a Comment