Skip to main content

Snippets

Numbers

GCD & LCM

const gcd = (a, b) => (a ? gcd(b % a, a) : b);

// LCM of two numbers
const lcm = (a, b) => (a * b) / gcd(a, b);

// LCM of an array of numbers
const lcm = (nums) => nums.reduce((a, b) => (a * b) / gcd(a, b));

Arrays

Combinations

const combinations = (array) =>
Array(1 << array.length)
.fill()
.map((_, i) => array.filter((_, j) => i & (1 << j)));