public void TestHexagonalNthNumber() { var expected = 40755; var actual = HexagonalNumberGenerator.NthNumber(143); Assert.AreEqual(expected, actual); }
static void Main(string[] args) { // all hexagonal numbers are also triangle numbers // only need to find next hexagonal number which is also // a pentagonal number int p = 166; int h = 144; BigInteger pNum = PentagonalNumberGenerator.NthNumber(p); BigInteger hNum = HexagonalNumberGenerator.NthNumber(h); while (pNum != hNum && pNum > 0) { if (pNum < hNum) { p++; pNum = PentagonalNumberGenerator.NthNumber(p); } else { h++; hNum = HexagonalNumberGenerator.NthNumber(h); } } Console.WriteLine(hNum); Console.ReadKey(); }