if 1 => 8, 2 => 18,
3 => 30, 4 => 44, 5 => 60, then what is 9?
It was simple to calculate by hand and provide the answer. You can also formulate the above deduction and use it for any number with
6 + (M + (2 x n))
where n is set of integers; 0 < n < N
and M is cumulative sum from previous step with M(initial value) is 0
This problem lends itself well to reduction function and with the given formula it was easy to check it for any integer n. Out of curiosity created a function and was called in Python for-loop for each iteration while monitoring load on CPU and memory tracked. Then I used reduce function with xrange to see the same.
>>> a = ; M = 0
>>> for n in range(0, 100000000):
... M = 6+M+(2*n)
# [ CPU was loaded above 97% with memory usage jumping and ended up killing the process ]
>>> print reduce(lambda x,y: 6+x+(2*y), xrange(0,100000000))
# with xrange, got the result in less than few seconds though CPU usage jumped above 97% but no discernible change in memory.