Sum until certain point – MySql

Came across this question while searching for my own answer. I thought I’d leave my solution here since it’s another way to accomplish the same task and may be more efficient. The trick is the self join using >= SELECT s1.ID, s1.name, s1.money, sum(s2.money) as accumulator FROM student s1 INNER JOIN student s2 ON s1.id … Read more

SSE reduction of float vector

Typically you generate 4 partial sums in your loop and then just sum horizontally across the 4 elements after the loop, e.g. #include <cassert> #include <cstdint> #include <emmintrin.h> float vsum(const float *a, int n) { float sum; __m128 vsum = _mm_set1_ps(0.0f); assert((n & 3) == 0); assert(((uintptr_t)a & 15) == 0); for (int i = … Read more

Quadratic algorithm for 4-SUM

Yes you can. Go over all pairs of numbers and store their sum(and also store which numbers give that sum). After that for each sum check if its negation is found among the sums you have. Using a hash you can reach quadratic complexity, using std::map, you will reach O(n^2*log(n)). EDIT: to make sure no … Read more

Sum values from an array of key-value pairs in JavaScript

You could use the Array.reduce method: const myData = [ [‘2013-01-22’, 0], [‘2013-01-29’, 0], [‘2013-02-05’, 0], [‘2013-02-12’, 0], [‘2013-02-19’, 0], [‘2013-02-26’, 0], [‘2013-03-05’, 0], [‘2013-03-12’, 0], [‘2013-03-19’, 0], [‘2013-03-26’, 0], [‘2013-04-02’, 21], [‘2013-04-09’, 2] ]; const sum = myData .map( v => v[1] ) .reduce( (sum, current) => sum + current, 0 ); console.log(sum); See … Read more