Triangle Number

  1. Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
    Triangle 1, 3, 6, 10, 15, …
    Pentagonal 1, 5, 12, 22, 35, …
    Hexagonal 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.

#include

using namespace std;

bool check(int n,long int tn )
{
long int hn=0,pn=0,i=0;
bool htrue=false,ptrue=false;
while(hn<=tn)
{
hn+=3i+1;
i++;
if(hn==tn)
htrue=true;
}
i=0;
while(pn<=tn)
{
pn+=4
i+1;
i++;
if(pn==tn)
ptrue=true;
}
if(htrue && ptrue)
return true;
return false;
}
int main()
{
long int tn=0;
bool x=false;
for(int i=1;;i++)
{
tn=tn+i;
if(i>285)
x=check(i,tn);
if(x)
{
cout<<"\nThe next triangle number which is also a hexagonal and pentagonal is "<<tn;
break;
}

}
return 0;

}

//