Sunday, 31 August 2014

Problem 44 Pentagon number project euler

int checkpent(long long int x)
{
float m=(1.0/6.0)+sqrt((1.0/36.0)+((2.0*x)/3));
long long int k=m;
if(k==m){return 1;}
else{return 0;}
}
long long int min=9000000;
main()
{
    long long int n=3000;

int a[n];
int i,j;
for(i=0;i<n;i++)
{
    a[i]=(i+1)*((3*i)+2)/2;
}
for(i=0;i<n;i++)
{
    for(j=i+1;j<n;j++)
    {
   int sub=abs(a[i]-a[j]);
   int sum=a[i]+a[j];
   if(checkpent(sub)==1&&checkpent(sum)==1){if(sub<min){min=sub;}
  }
    }
}
printf("%d",min);
}

No comments:

Post a Comment