// Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
// Triangle Tn = n(n+1)/2 1, 3, 6, 10, 15, ...
// Pentagonal Pn = n(3n-1)/2 1, 5, 12, 22, 35, ...
// Hexagonal Hn = n(2n-1) 1, 6, 15, 28, 45, ...
// It can be verified that T285 = P165 = H143 = 40755.
// Find the next triangle number that is also pentagonal and hexagonal.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
namespace practice
{
class Program
{
static void Main(string[] args)
{
List<long> triangles = GetTriangles();
long n = 40756;
for(int i = 287; i < triangles.Count; i++){
long k = triangles[i];
if(CheckPentagonal(k) && CheckHexagonal(k)){
n = k;
break;
}
}
Console.Write(n);
}
static List<long> GetTriangles()
{
List<long> triangles = new List<long>();
for (long i = 1; i < 10000000; i++)
{
long t = (i * (i + 1)) / 2;
triangles.Add(t);
}
return triangles;
}
static bool CheckPentagonal(long k)
{
double n = (Math.Sqrt(24 * k + 1) + 1) / 6;
return n == (long)n;
}
static bool CheckHexagonal(long k)
{
double n = (Math.Sqrt(8 * k + 1) + 1) / 4;
return n == (long)n;
}
}
}