Using recursion to calculate cumulative values
We often need to calculate cumulative values such as cumulative sales amount or gross profit. There are typically two types of cumulative calculations, sum-based and multiplication-based. Cumulative sales amount or gross profit is sum-based; gross profit margin expressed in percentage is multiplication-based.
There are two approaches to calculate cumulative values. One approach is to use recursive technique; another approach uses a range technique.
For sum-based cumulative values, both of these approaches should produce the same result.
For multiplication-based cumulative values, the recursive technique will still work. In addition, we will show another approach that uses a mathematical rule about logarithm function. A known mathematical rule says that the logarithm of a product is equal to the sum of individual logarithms.
Let's see how we can use the recursive technique, the range technique, and the logarithm rule to calculate cumulative values...