I am interested in calculating the sequence of triangles 1 which is a sequence of pairs (i, j): (0, 0), (1, 0), (1, 1), (2, 0), (2, 1) ... which is repeated, although all pairs (i, j) with the restriction i >= j . The same sequence c is interesting, but with the restriction i> j.
These sequences, among other things, are all ways to select 2 (possibly identical) elements from a set of n-elements (for a sequence up to (n, n) 2 ) or indices of the lower triagular elements of matrix 3 . The sequence of values ​​is only for i A003056 in OEIS, while j is A002262 . This sequence often occurs in combinatorial algorithms, where their performance can be critical.
A simple but branched way to generate the following value in a sequence:
if (i == j) { j = 0; i++; } else { j++; } }
However, when calculating the initial elements of the sequence when checking the condition (i == j) - this suffers from many incorrect predictions, usually one incorrect prediction every time i increases. As the sequence increases, the number of erroneous forecasts becomes smaller, since i increases with a reduced frequency, therefore, the j++ branch dominates and is well predicted. However, some types of combinatorial searches are repeated many times by small terms in a sequence, so I'm looking for a branchless approach or some other approach that suffers less error errors.
For many applications, the order of the sequences is not so important, so generating differnet values ​​than mentioned above is acceptable if it leads to a better solution. For example, j can calculate, not up: (0, 0), (1, 1), (1, 0), (2, 2), (2, 1), ...
1 I am also interested to know what is the correct name for this sequence (perhaps that’s why I better named this question). I'm just kind of a "triangular sequence."
2 Here version i >= j represents subsets (repetition is allowed), and option i > j represents normal subsets (without repetition).
3 Here version i >= j includes the main diagonal, while version i > j excludes it.