I appreciate the nice programming aspects but cannot help by noting that taking the limit of n to infinity is even nicer:

sum= 1 + x^1 + x^2 + x^3 + ... = 1 + x*( 1 + x^1 + x^2 + ...)

...