code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

03 Largest Prime Factor

function largestPrimeFactor(number) {
  const factors = [];
  let d = 2;
  while (number > 1) {
    if (number % d === 0) {
      factors.push(d);
      number /= d;
    }
    d += 1;
  }
  return Math.max(...factors);
}

console.log(largestPrimeFactor(13195));

Created 2019-11-24T04:53:20+05:30 · Edit