tags:

views:

82

answers:

3

given

d0=0
d1=5+3d0
d2=5+3d1
...
dn=5+3dn-1

how would I write the summation for this up to n?

A: 
int x = 0;
for (int i = 0; i <= n; ++i)
{
   x += 3*x + 5;
}

x -= 1;
duffymo
I think that the "-1" in "dn=5+3dn-1" refers to the index of d (i.e. n-1) and not the total as you have it coded.
andand
A: 
5n + 3d(n(n-1)/2)
Ignacio Vazquez-Abrams
+1  A: 

Here's a more general problem and solution. Let f(x) = ax + b. (In your case x = 0, a = 3, and b = 5.) If you iterate f(x) n times, i.e. f(f(f...(x) ...)) with n f's, you get

a^n x + b(1 + a + a^2 + ... + a^(n-1))

The sum (1 + a + a^2 + ... + a^(n-1)) can be reduced to (a^n - 1) / (a - 1) if a != 1.

John D. Cook