class Resistor { #ohmage; constructor(ohmage) { this.#ohmage = ohmage; } getOhmage() { return this.#ohmage; } equals(other) { return this.getOhmage() === other.getOhmage(); } hash() { return this.#ohmage; } size() { return 1; } toString() { return `${this.constructor.name}(${this.getOhmage()})`; } prettyString(depth = 0) { return `${'--'.repeat(depth)}${this.toString()}` } } class ComplexResistor extends Resistor { left; constructor(other) { super(other.getOhmage()); this.left = other; } } class SeriesResistor extends Resistor { left; right; constructor(r1, r2) { super(r1.getOhmage() + r2.getOhmage()); this.left = r1; this.right = r2; } size() { return this.left.size() + this.right.size(); } prettyString(depth = 0) { var leftString = this.left.prettyString(depth + 1) var leftString = this.left.constructor.name === "SeriesResistor" ? `${this.left.left.prettyString(depth + 1)}\n${this.left.right.prettyString(depth + 1)}` : this.left.prettyString(depth + 1); var rightString = this.right.constructor.name === "SeriesResistor" ? `${this.right.left.prettyString(depth + 1)}\n${this.right.right.prettyString(depth + 1)}` : this.right.prettyString(depth + 1); return `${'--'.repeat(depth)}${this.toString()}\n${leftString}\n${rightString}`; } } class ParallelResistor extends Resistor { left right constructor(r1, r2) { super(1 / ((1/r1.getOhmage()) + (1/r2.getOhmage()))); this.left = r1; this.right = r2; } size() { return this.left.size() + this.right.size(); } prettyString(depth = 0) { return `${'--'.repeat(depth)}${this.toString()} ${this.left.prettyString(depth + 1)} ${this.right.prettyString(depth + 1)}` } } var resistors = [100] var N = 4 var maxValue = Math.max(...resistors) numberline = new Array(N * maxValue + 1) numberline.fill(-1) set = new Map() for (i = 0; i < resistors.length; i++) { var value = resistors[i]; var resistor = new Resistor(value); set.set(value, resistor); numberline[value] = resistor; } for (i = 0; i < N; i++) { iterSet(); console.log("After iteration", i, set.size, "entries"); } console.log(set) function iterSet() { addition = new Set() set.forEach(e1 => { set.forEach(e2 => { r1 = new SeriesResistor(e1, e2); r2 = new ParallelResistor(e1, e2); //console.log(addition.size) addition.add(r1); addition.add(r2); }); }); addition.forEach( e => { value = Math.round(e.getOhmage()); if (!set.has(value)) { set.set(value, e) numberline[value] = e; } }); } function missings() { missing = new Set() for (i = 0; i < N * maxValue + 1; i++) { if (numberline[i] == -1) missing.add(i); } return missing; }