The one glaring error is this:
int64_t maxPower = 50; int64_t results[maxPower]; // <-- Not really legal, but let's pretend //... for (int64_t power = 0; power <= maxPower; // <-- Look at the loop constraints power++) { //.... } results[power] = value; // <-- Buffer overrun
Since maxPower
is 50, you are accessing an element that’s out of bounds. This is undefined behavior
Also, to make the code legal C++, either declare an array of 50:
int64_t results[50];
or declare a std::vector<int64_t>
.
When you do that and use at()
instead of []
, then you get a better view of how your program breaks. See the Live Example using std::vector here.
As to the “odd number”, you are getting a numeric overflow here:
int64_t value = pow(tuple, power);
If tuple
is 3, and power
is 41
, then the value overflows the int64_t
type.
Maybe you need to use an arbitrary precision library (one that preferably has overloaded the requisite mathematical operators) or write your own, instead of using the “regular” C++ types. Then you won’t or shouldn’t overflow.