0
a FUNCTION seriessum() to solve the series 1+(1+2)+(1+2+3)+(1+2+3+4)+.........n
without array
6 Answers
+ 2
unsigned sum = 0;
for(unsigned i=0; i<=n; ++i)
{
sum = sum*2 + i;
}
0
didn't check, just from the mind, something like this?? : for(int i=1; i<=n;++I) sum+= I * (n -I +1);
0
I would do it like this:
unsigned sum = 0;
for (unsigned i=0; i<n; ++i)
{
sum += (n-i)*(i+1);
}
0
n!
0
int result = (n*n*n+3*n*n+2*n)/6;
actually not safe from overflow.