Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a cumulative sequence?

Tags:

Say I have a lazy sequence like the following:

(def s (iterate inc 1)) (take 10 s) => (1 2 3 4 5 6 7 8 9 10) 

Now, I want to generate a sequence of cumulative sum of s like the following:

=> (1 3 6 10 15 ...) 

How can I do this? What I tried is to use atom and accumulate the sum to it(mutating) Is this the only way to generate cumulative sequence or is there a better way to do this?

NOTE: the above cumulative sum is only an example. The source sequence can be other sequence. So I can't use formula: s(n) = n(n+1)/2

like image 514
ntalbs Avatar asked Feb 07 '13 15:02

ntalbs


People also ask

What is a cumulative sequence?

A cumulative sum is a sequence of partial sums of a given sequence. For example, the cumulative sums of the sequence.

How do you calculate cumulative?

The cumulative frequency is calculated by adding each frequency from a frequency distribution table to the sum of its predecessors. The last value will always be equal to the total for all observations, since all frequencies will already have been added to the previous total.

How do I do a cumulative formula in Excel?

Create a running total formula. In our sample Excel workbook, let's say you want a cumulative total posted in column C. In cell C1, you would type =SUM($B$2:B2). This creates the necessary relative reference point (B2) and absolute reference point ($B$2) for your running tally.

What is cumulative sum formula?

Explanation: the simple formula =C2+B3 changes to =C3+B4, =C4+B5, etc. Each time the new value is added to the previous running total.


1 Answers

(take 10 (reductions + s)) => (1 3 6 10 15 21 28 36 45 55) 
like image 169
A. Webb Avatar answered Sep 22 '22 03:09

A. Webb