its going to overflow relatively soon but I see nothing wrong with the problem.
using little data types (float instead of double or if available, long double) and int instead of int64 is not helping you with the potential to overflow.
do you have a solved case you can test against, eg 3 and 5 gives 42 or whatever (yes, I know that is not right).
just say x*x pow is not helpful for small int powers (its slower, not that it matters here I guess .. you will notice if you do millions of them)
its possible your code is right and just overflowed depending on the inputs. try like mul start at 1, a being 2, N being like 5 or something really simple.
style: typically one says 0 to N in c++
for(int i = 0; i < N; i++) //same as <=N starting at 1, but its convention to do it this way unless you use i in the loop and its value matters.
style: x serves no purpose. read into mul directly.
pretty sure its just overflow. Note that 2,0,5 is 4.blah e9 ... so it gets huge rapidly. My no-frills take:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int main()
{
double x,a;
unsigned int n;
cin >> x >> a >> n;
for(int i = 0; i < n; i++)
{
x+=a;
x*=x;
}
cout << x << endl;
}
|