given that every other triangular number starting from 1 is a hexagonal number and every pentagonal number is 1/3 of a triangular number it follows that if a number is both hexagonal and pentagonal it must be triangular. I have the following to find the first triangular number other than 1 that is pentagonal and hexagonal as well
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isPentagonal( unsigned long );
int main()
{
for ( unsigned long hex, i = 2; ; i++ )
{
hex = ( i * ( 2 * i - 1) );
printf("%lu = %lu\n ", i , hex);
if ( isPentagonal( hex ) )
{
printf("%lu\n", i);
break;
}
}
return 0;
}
int isPentagonal( unsigned long n )
{
int sq = sqrt( 1 + 24 * n);
return sq*sq == ( 1 + 24 * n ) && ( 1 + sq ) % 6 == 0;
}
it works i get the answer 143 which is correct
However, it then goes into an infinite loop if i is greater than 143 rather than find the next one