2 ms·
I suppose if you're not bothered with wasting lots of time, do the following for a precise sum: Select the two smallest numbers from the list, remove them and i
by _0ffh 5y ago
I suppose if you're not bothered with wasting lots of time, do the following for a precise sum: Select the two smallest numbers from the list, remove them and insert their sum. Repeat until only one number is left.
[Ed. smaller meaning closer to zero]
- dolmen 5y agoThis is exactly what happens when you sort the number ascending and compute the sum starting from the smallest. Like in the article. Except this doesn't work if you must handle negative numbers. So what matters is not the smallest numbers but the number nearer from zero.
- MauranKilom 5y agoNo, that's not the same. Summing [4, 5, 6, 7] would yield the series (4, )9, 15, 22 with "sort then add", but what the previous poster described would first sum 4+5 (leaving the list [9, 6, 7]), then 6+7 (leaving the list [9, 13]) and finally arriving at 22.
- dolmen 5y agoOK. That algorithm could be improved (for accuracy, not speed) by replacing sums of the smallest number with multiplications as I suppose that multiplication of more than 3 identical numbers or more is loosing less precision than multiple additions. For example: [4, 3, 7, 7, 8] could be calculated as: (3+4) => [7, 7, 7, 8] (7*3) => [21, 8] (8+21) => 29