-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem006.cs
53 lines (45 loc) · 1.39 KB
/
Problem006.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
namespace ProjectEuler.Solutions
{
using System;
using NUnit.Framework;
/// <summary>
/// Sum square difference.
/// The sum of the squares of the first ten natural numbers is,
/// <para>1<sup>2</sup> + 2<sup>2</sup> + ... + 10<sup>2</sup> = 385</para>
/// The square of the sum of the first ten natural numbers is,
/// <para>(1 + 2 + ... + 10)<sup>2</sup> = 55<sup>2</sup> = 3025</para>
/// 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.
/// <para>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</para>
/// </summary>
public class Problem006 : Problem
{
public override long Solution()
{
return SumSquareDifference(100);
}
private static long SumSquareDifference(int upperBound)
{
double sumOfSquares = 0;
double squareOfSums = 0;
for (int number = 1; number <= upperBound; number++)
{
sumOfSquares += Math.Pow(number, 2);
squareOfSums += number;
}
squareOfSums = Math.Pow(squareOfSums, 2);
double difference = squareOfSums - sumOfSquares;
return (long)difference;
}
[Test]
public void TestForExample()
{
long result = SumSquareDifference(10);
Assert.AreEqual(2640, result);
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(25164150, this.Solution());
}
}
}