Sum[r^(k^2), {k, 0, n}]