Your C implementation, including its math library, has a bad implementation of pow
that returns inaccurate results. (A good pow
implementation returns an exact result when the mathematical result is exactly representable in the floating-point format.) For example, for pow(7, 2)
, your pow
may be returning a value slightly under 49. When this is converted to an unsigned int
(in the addition to sum
), it is truncated, resulting in 48.
To work around this defect in your library, do not use pow
for integer arithmetic. Write your own integer arithmetic to get the result you desire. In some circumstances, using round(pow(N, n))
may suffice, but there are considerations of the range of values for which this will work (wide integers may be able to represent integers that double
cannot) and sometimes performance. (Another workaround is to get a better C implementation.)