what is the largest prime factor of the number 600851475143 in javascript

index.tsx
function largestPrimeFactor(number) {
  let divisor = 2;

  while (number > 1) {
    if (number % divisor == 0) {
      number = number / divisor;
    } else {
      divisor++;
    }
  }

  return divisor;
}

console.log(largestPrimeFactor(600851475143)); // Output: 6857
272 chars
16 lines

Explanation: We start with the smallest prime factor and keep dividing the number by it until the number is no longer divisible by that factor. Then we move on to the next prime factor and repeat the process until we reach the largest prime factor. Finally, we return the largest prime factor which is the result of the algorithm.

gistlibby LogSnag