iters.js

View source code here on GitHub!

iters.combinations(iterable, r)

Iterate over the different combinations of the elements in a given iterable

Note that this is a direct port of the recipe given in python's itertools

Arguments:
  • iterable (Iterable.<T>)

  • r (number)

iters.combinationsWithReplacement(iterable, r)

Iterate over the different combinations of the elements in a given iterable

Note that this is a direct port of the recipe given in python's itertools

Arguments:
  • iterable (Iterable.<T>)

  • r (number)

iters.abundants(stop)

Iterate over the abundant numbers

Arguments:
  • stop (null|number)

 1/**
 2 * Iterate over the different combinations of the elements in a given iterable
 3 *
 4 * Note that this is a direct port of the recipe given in python's itertools
 5 * @param {Iterable.<T>} iterable
 6 * @param {number} r
 7 * @yield {T}
 8 */
 9exports.combinations = function* combinations(iterable, r) {
10    const pool = Array.from(iterable);
11    const n = pool.length;
12    if (r > n) {
13        return;
14    }
15    const indices = [...Array(r).keys()];
16
17    yield Array.from(indices.map((i) => pool[i]));
18    while (true) {
19        let broken = false;
20        let i = r - 1;
21        for (; i > -1; i--) {
22            if (indices[i] !== i + n - r) {
23                broken = true;
24                break;
25            }
26        }
27        if (!broken) {
28            return;
29        }
30        indices[i] += 1;
31        for (let j = i + 1; j < r; j += 1) {
32            indices[j] = indices[j-1] + 1;
33        }
34        yield Array.from(indices.map((i) => pool[i]));
35    }
36};
37
38/**
39 * Iterate over the different combinations of the elements in a given iterable
40 *
41 * Note that this is a direct port of the recipe given in python's itertools
42 * @param {Iterable.<T>} iterable
43 * @param {number} r
44 * @yield {T}
45 */
46exports.combinationsWithReplacement = function* combinationsWithReplacement(iterable, r) {
47    const pool = Array.from(iterable);
48    const n = pool.length;
49    if (r > n) {
50        return;
51    }
52    const indices = [...Array(r).keys()];
53
54    yield Array.from(indices.map((i) => pool[i]));
55    while (true) {
56        let broken = false;
57        let i = r - 1;
58        for (; i > -1; i--) {
59            if (indices[i] !== i + n - r) {
60                broken = true;
61                break;
62            }
63        }
64        if (!broken) {
65            return;
66        }
67        indices.fill(indices[i] + 1, i, r);
68        yield Array.from(indices.map((i) => pool[i]));
69    }
70};
71
72/**
73 * Iterate over the abundant numbers
74 * @param {null | number} stop
75 * @yield {number}
76 */
77exports.abundants = function* abundants(stop = null) {
78    for (let x = 12; !stop || x < stop; x++) {
79        let sum = 0;
80        for (const y of factors.properDivisors(x)) {
81            sum += y;
82        }
83        if (sum > x) {
84            yield x;
85        }
86    }
87};
88
89const factors = require('./factors.js');