How to use the lcm function from mathjs

Find comprehensive JavaScript mathjs.lcm code examples handpicked from public code repositorys.

mathjs.lcm function calculates the least common multiple of two or more numbers.

99
100
101
102
103
104
105
106
107
108
109


function makeMaths(freqsArray) {
	return new Promise((resolve) => {
		const gcd = math.gcd(...freqsArray)
		const freqsDividedByGCD = freqsArray.map(freq => freq / gcd)
		const lcd = math.lcm(...freqsDividedByGCD)
		resolve(lcd)
	})
}

fork icon0
star icon6
watch icon1

+ 7 other calls in file

55
56
57
58
59
60
61
62
63
64
math.ceil(a); // round in positive direction
math.fix(a) // round towards zero

// factors & multiples
math.gcd(a, b) // greatest common denominator
math.lcm(a, b) // least common multiple

// stats
math.max(numbers);
math.min(numbers);
fork icon0
star icon3
watch icon0

+ 6 other calls in file

How does mathjs.lcm work?

mathjs.lcm is a function that computes the least common multiple (LCM) of two or more integers by finding the smallest positive integer that is divisible by each of the input integers. It uses the prime factorization of the input integers to compute their LCM. If the input arguments are not integers or there are no input arguments, it returns null.

93
94
95
96
97
98
99
100
101
102
103
104
105


    state = runIteration(state);
    step += 1;
  }


  return math.lcm(...reps);
}


function cleanInput(input) {
  return input
fork icon1
star icon0
watch icon0

65
66
67
68
69
70
71
72
73
74
}
const HEIGHT = grid.length;
const WIDTH = grid[0].length;
const IN_HEIGHT = HEIGHT - 2;
const IN_WIDTH = WIDTH - 2;
const LCM = math.lcm(IN_HEIGHT, IN_WIDTH);
const GOAL_ROW = IN_HEIGHT;
const GOAL_COL = IN_WIDTH;
let states = [];
states[0] = structuredClone(positions);
fork icon0
star icon1
watch icon1

Ai Example

1
2
3
4
5
6
7
8
const math = require("mathjs");

const a = 12;
const b = 18;

const lcm = math.lcm(a, b);

console.log(lcm); // Output: 36

In the example above, we first import the mathjs library and then define two variables a and b with values 12 and 18 respectively. We then call the math.lcm function and pass in the two variables to find their least common multiple. The result is stored in the lcm variable and printed to the console, which is 36 in this case.

55
56
57
58
59
60
61
62
63
64
            : (last_monkey.targets.false = Number(tokens[5]));
          break;
      }
    }
  });
  let lcm = math.lcm(...this.monkeys.map((m) => m.test));
  this.monkeys.forEach((monkey) => (monkey.lcm = lcm));
}

run() {
fork icon0
star icon1
watch icon0

124
125
126
127
128
129
130
131
132
133
const newNode = node.cloneDeep();

const denominators = newNode.args.map(fraction => {
  return parseFloat(fraction.args[1].value);
});
const commonDenominator = math.lcm(...denominators);

newNode.args.forEach((child, i) => {
  // missingFactor is what we need to multiply the top and bottom by
  // so that the denominator is the LCD
fork icon0
star icon0
watch icon0