The number D computed by the following polynomial formula
D=(pow(i,5.)/120)-(pow(i,4.)/12)+(23*pow(i,3.)/24)-(83*i*i/12)+(571*i/30)
should always be an integer, when i=1,2,3,4,5,....
However, for some i values, each separate term may not be an integer, but when putting together (with additions and subtractions) in the above formula, any non-integer parts would be "cancelled out", and the result should be precisely an integer.
The problem is, using this formula directly, the resulting numbers are sometimes inaccurate (for some i values), with the last digit 1 off (+1 or -1) from what it should be.
So I modified the code as follows, which gets better (less errors), but still not 100% accurate (never mind the long long type--it's for when i gets large):
long long v=pow(i,5.), w=pow(i,4.), x=23*pow(i,3.), y=83*i*i, z=571*i, vv,ww,xx,yy,zz;
if (v%120!=0)
{
if (v/120.<((floor(v/120.)+ceil(v/120.))/2.))
vv=floor(v/120.);
else
vv=ceil(v/120.);
}
else
vv=v/120;
if (w%12!=0)
{
if (w/12.<((floor(w/12.)+ceil(w/12.))/2.))
ww=floor(w/12.);
else
ww=ceil(w/12.);
}
else
ww=w/12;
if (x%24!=0)
{
if (x/24.<((floor(x/24.)+ceil(x/24.))/2.))
xx=floor(x/24.);
else
xx=ceil(x/24.);
}
else
xx=x/24;
if (y%12!=0)
{
if (y/12.<((floor(y/12.)+ceil(y/12.))/2.))
yy=floor(y/12.);
else
yy=ceil(y/12.);
}
else
yy=y/12;
if (z%30!=0)
{
if (z/30.<((floor(z/30.)+ceil(z/30.))/2.))
zz=floor(z/30.);
else
zz=ceil(z/30.);
}
else
zz=z/30;
long long D=v/120-w/12+x/24-y/12+z/30-16;
Hope it's clear to you the idea behind this code: it means to "cancel out" non-integer parts by truncating or rounding up the non-integer term according to whether the digit after the decimal point is less than 5 or not.
It still has errors (e.g. when i=6, D should be 29, but the output is 28; when i=12, D should be 1234, but the output is 1233; etc.).
I just realized the problem is probably when there are two terms both being exactly __.5 (the digit after the decimal point is 5), in which case both would be rounded up (ceil) to 1 by my program above, so when adding them the last digit would become 1+1=2, when it should be .5+.5=1.
When thinking more, it seem quite complicated even if to add more if...else... Any way to solve the problem?
Thanks in advance!