The sum of the squares of the first ten natural numbers is,
1² + 2² + ... + 10² = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)² = 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.
This problem is easy in both C# and F#
F#
let sum lst = List.fold_left (+) 0 lst
let sqr i = i * i
let sumofsquares = sum (List.map sqr [1..100])
let sumsquared = sqr ( sum [1..100])
printf "%i" (sumsquared - sumofsquares)
C#
class Program
{
public static decimal sqr(int i)
{
return i * i;
}
static void Main(string[] args)
{
decimal sumofsquares = Enumerable.Range(1, 100).Sum(i => i * i);
decimal sumsquared = sqr(Enumerable.Range(1, 100).Sum());
Console.WriteLine(sumsquared - sumofsquares);
Console.ReadLine();
}
}
del.icio.us Tags:
C#,
F#,
Project Euler