Skip to content

Commit

Permalink
Satisfy ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 22, 2024
1 parent 9f33891 commit ab3d1d2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 79 deletions.
2 changes: 1 addition & 1 deletion docs/src/javascript/lib/iters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ View source code :source:`javascript/src/lib/iters.js`

.. js:autofunction:: iters.combinations

.. js:autofunction:: iters.combinations_with_replacement
.. js:autofunction:: iters.combinationsWithReplacement

.. js:autofunction:: iters.abundants

Expand Down
2 changes: 1 addition & 1 deletion docs/src/javascript/p0060.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ View source code :source:`javascript/src/p0060.js`

.. js:autofunction:: p0060

.. js:autofunction:: is_concat_prime
.. js:autofunction:: isConcatPrime

.. literalinclude:: ../../../javascript/src/p0060.js
:language: javascript
Expand Down
24 changes: 12 additions & 12 deletions javascript/src/lib/iters.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* 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
* @param {Iterable.<T>} iterable
* @param {number} r
* @yield {T}
*/
exports.combinations = function*(iterable, r) {
let pool = Array.from(iterable);
exports.combinations = function* combinations(iterable, r) {
const pool = Array.from(iterable);
const n = pool.length;
if (r > n) {
return;
}
indices = [...Array(r).keys()];

yield Array.from(indices.map(i => pool[i]));
yield Array.from(indices.map((i) => pool[i]));
while (true) {
let broken = false;
let i = r - 1;
Expand All @@ -31,27 +31,27 @@ exports.combinations = function*(iterable, r) {
for (let j = i + 1; j < r; j += 1) {
indices[j] = indices[j-1] + 1;
}
yield Array.from(indices.map(i => pool[i]));
yield Array.from(indices.map((i) => pool[i]));
}
};

/**
* 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
* @param {Iterable.<T>} iterable
* @param {number} r
* @yield {T}
*/
exports.combinations_with_replacement = function*(iterable, r) {
let pool = Array.from(iterable);
exports.combinationsWithReplacement = function* combinationsWithReplacement(iterable, r) {
const pool = Array.from(iterable);
const n = pool.length;
if (r > n) {
return;
}
indices = [...Array(r).keys()];

yield Array.from(indices.map(i => pool[i]));
yield Array.from(indices.map((i) => pool[i]));
while (true) {
let broken = false;
let i = r - 1;
Expand All @@ -65,7 +65,7 @@ exports.combinations_with_replacement = function*(iterable, r) {
return;
}
indices.fill(indices[i] + 1, i, r);
yield Array.from(indices.map(i => pool[i]));
yield Array.from(indices.map((i) => pool[i]));
}
};

Expand All @@ -74,7 +74,7 @@ exports.combinations_with_replacement = function*(iterable, r) {
* @param {null | number} stop
* @yield {number}
*/
exports.abundants = function*(stop = null) {
exports.abundants = function* abundants(stop = null) {
for (let x = 12; !stop || x < stop; x++) {
let sum = 0;
for (const y of factors.properDivisors(x)) {
Expand All @@ -84,6 +84,6 @@ exports.abundants = function*(stop = null) {
yield x;
}
}
}
};

const factors = require('./factors.js');
2 changes: 1 addition & 1 deletion javascript/src/p0005.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
**/
exports.p0005 = function() {
const group = [...Array(21).keys()].slice(1);
let answer = 1_000_000_000_000;
let answer = 1000000000000;
for (const x of group) {
for (const multiples of iters.combinations(group, x)) {
const num = multiples.reduce((a, x) => a * x, 1);
Expand Down
8 changes: 4 additions & 4 deletions javascript/src/p0023.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
* @return {number}
**/
exports.p0023 = function() {
const abundant_sums = new Set([24]);
const abundantSums = new Set([24]);
const abundants = [...iters.abundants(28112)];
for (const x of abundants) {
for (const y of abundants) {
abundant_sums.add(x + y);
abundantSums.add(x + y);
}
}
let sum = 0;
for (let x = 1; x < 28124; x++) {
if (abundant_sums.has(x)) {
if (abundantSums.has(x)) {
sum += x;
}
}
return sum
return sum;
};

const iters = require('./lib/iters.js');
121 changes: 61 additions & 60 deletions javascript/src/p0060.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
/**
* Project Euler Problem 60
*
* Problem:
*
* The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the
* result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes,
* 792, represents the lowest sum for a set of four primes with this property.
*
* Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
*
* @return {number}
*/
exports.p0060 = function() {
const iterator = primes.primes();
const cached_primes = [3];
iterator.next(); // 2 is excluded because higher even numbers can't be prime
iterator.next();
iterator.next(); // 5 is excluded because if a number ends with 5, it's divisible by 5
const compat = new Map();
for (const x of iterator) {
for (const y of cached_primes) {
if (is_concat_prime(x, y)) {
for (const [a, b, c] of iters.combinations(compat.get(y) || new Set(), 3)) {
if (compat.get(b).has(a) && compat.get(c).has(a) && compat.get(c).has(b)) { // remember these are commutative
if (is_concat_prime(a, x) && is_concat_prime(b, x) && is_concat_prime(c, x)) {
return x + y + a + b + c;
}
}
}
if (!compat.has(x)) {
compat.set(x, new Set());
}
compat.get(x).add(y);
if (!compat.has(y)) {
compat.set(y, new Set());
}
compat.get(y).add(x);
}
}
cached_primes.push(x);
}
return -1;
};

/**
* Tests if a pair of numbers generates a prime if concatenated in both orders.
*
* @param {number} x
* @param {number} y
* @return {number}
*/
const is_concat_prime = function(x, y) {
const sx = x.toString();
const sy = y.toString();
return primes.isPrime(parseInt(sx + sy)) && primes.isPrime(parseInt(sy + sx));
};

const iters = require('./lib/iters.js');
const primes = require('./lib/primes.js');
/**
* Project Euler Problem 60
*
* Problem:
*
* The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the
* result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four
* primes, 792, represents the lowest sum for a set of four primes with this property.
*
* Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
*
* @return {number}
*/
exports.p0060 = function() {
const iterator = primes.primes();
const cachedPrimes = [3];
iterator.next(); // 2 is excluded because higher even numbers can't be prime
iterator.next();
iterator.next(); // 5 is excluded because if a number ends with 5, it's divisible by 5
const compat = new Map();
for (const x of iterator) {
for (const y of cachedPrimes) {
if (isConcatPrime(x, y)) {
for (const [a, b, c] of iters.combinations(compat.get(y) || new Set(), 3)) {
// remember these checks are commutative
if (compat.get(b).has(a) && compat.get(c).has(a) && compat.get(c).has(b)) {
if (isConcatPrime(a, x) && isConcatPrime(b, x) && isConcatPrime(c, x)) {
return x + y + a + b + c;
}
}
}
if (!compat.has(x)) {
compat.set(x, new Set());
}
compat.get(x).add(y);
if (!compat.has(y)) {
compat.set(y, new Set());
}
compat.get(y).add(x);
}
}
cachedPrimes.push(x);
}
return -1;
};

/**
* Tests if a pair of numbers generates a prime if concatenated in both orders.
*
* @param {number} x
* @param {number} y
* @return {number}
*/
const isConcatPrime = function(x, y) {
const sx = x.toString();
const sy = y.toString();
return primes.isPrime(parseInt(sx + sy)) && primes.isPrime(parseInt(sy + sx));
};

const iters = require('./lib/iters.js');
const primes = require('./lib/primes.js');

0 comments on commit ab3d1d2

Please # to comment.