Iterative sum function