-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem045.cs
52 lines (47 loc) · 1.31 KB
/
Problem045.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
namespace ProjectEuler.Solutions
{
using NUnit.Framework;
using ProjectEuler.Helper;
/// <summary>
/// Triangular, pentagonal, and hexagonal.
/// Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
/// <para>Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ...</para>
/// <para>Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, ...</para>
/// <para>Hexagonal Hn=n(2n−1) 1, 6, 15, 28, 45, ...</para>
/// It can be verified that T285 = P165 = H143 = 40755.
/// <para>Find the next triangle number that is also pentagonal and hexagonal.</para>
/// </summary>
public class Problem045 : Problem
{
public override long Solution()
{
long number;
for (int i = 144;; i++)
{
long hexagonalNumber = GetHexagonalNumber(i);
if (Numbers.IsPentagonNumber(hexagonalNumber) && Numbers.IsTriangleNumber(hexagonalNumber))
{
number = hexagonalNumber;
break;
}
}
return number;
}
private static long GetHexagonalNumber(int n)
{
return n * ((2 * n) - 1);
}
[Test]
public void TestForExample()
{
Assert.IsTrue(Numbers.IsTriangleNumber(40755));
Assert.IsTrue(Numbers.IsPentagonNumber(40755));
Assert.AreEqual(40755, GetHexagonalNumber(143));
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(1533776805, this.Solution());
}
}
}