Lotus Notes FAQ Visit Our Sponsor!

How do you sum a numberlist?

Divide and Conquer Method

Have you tried adding two list of numbers together? 1:1:1:1 + 1:1:1:1 = 2:2:2:2. Now lets split the resultant list and add the two list together. 2:2 + 2:2 = 4:4. Now do it again. 4 + 4 = 8. So if you start with the list 1:1:1:1:1:1:1:1 and use this process you would get your answer of 8. That's all there is to it. Obviously your list has to contain a number of elements equal to a power of 2. That's the first thing to fix.
There is a self imposed limit of 8192 elements that can be added together. The formula can be extended to add together millions of elements but there must always be a limit. 8192 was chosen for two reasons: 1) A number takes 8 bytes in LN; a lookup can only return 64k, therefore only 8192 numbers will fit into 64k. 2) A field on a form has the same limit.
Put the list in a field called values and pass it to the formula:
list := 0;
adjust := 0;
elem := @Elements(values);
@If(elem = 1; @Return(values); elem > 8192; 
        @Do(@Prompt([OK]; "Error"; "Number of elements to add exceeded 8192"); 
@Return("")); "");
f := @Power(2; @Integer((@Log(elem) / @Log(2))));
@Set("adjust"; elem - f);
@If(adjust = 0; @Set("list"; values); 
        @Do(@Set("list"; ((@Subset(values; adjust) + @Subset(values; -adjust)) : @Subset(@Subset(values; f); adjust - f)))));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
@If(f > 1; @Do(@Set("f"; f / 2); @Set("list"; ((@Subset(list; f) + @Subset(list; -f))))); @Return(list));
list

Use a Math Library

There is a math library that has a sum function which is hooked in via @dblookup. Click [ftp] here to download it. Notes 4.0 will also have a built-in @sum function.

Applies to Notes Versions: 3
Last Modified: April 17, 1996