Everytime you're outputting the "sum", which accumulates all your terms, so this is why you get higher and higher values everytime. At some point, you need to stop accumulating the past values, and...