factors.js

View source code here on GitHub!

factors.properDivisors(num)

Iterates over the proper divisors of a given number

Arguments:
  • num (number)

 1/**
 2 * Iterates over the proper divisors of a given number
 3 * @param {number} num
 4 * @yield {number}
 5 */
 6function* properDivisors(num) {
 7    const factors = [...primes.primeFactors(num)];
 8    const seen = new Set();
 9    yield 1;
10    for (let x = 1; x < factors.length; x++) {
11        for (const combo of iters.combinations(factors, x)) {
12            const ret = combo.reduce((a, x) => a * x, 1);
13            if (!seen.has(ret)) {
14                yield ret;
15                seen.add(ret);
16            }
17        }
18        seen.clear();
19    }
20}
21exports.properDivisors = properDivisors;
22
23const iters = require('./iters.js');
24const primes = require('./primes.js');