Finding the largest prime factor of a number in JavaScript


In the given problem statement we have to find the largest prime factor of a number with the help of Javascript functionalities. So we will solve this problem with the help of basic functionalities of Javascript.

Understanding the Problem

The problem at hand is to find the largest prime factor of a given input number. So the prime factor of a number is the prime number which can divide the given number without leaving the remainder. Our task is to find the largest prime factor which is the prime factor with the highest value. For example let's say we have a number 84. So the prime factors of this number are 2, 2, 3 and 7. In these numbers the largest prime factor is 7.

Logic for the given Problem

To solve this problem we will use a method called prime factorization. So basically we will start the process by dividing the given number by the smallest prime number that is 2 and continue this process until it is not divided by 2. After that we will move on the next prme number and continue the process until we will reach at a point where the given number is equal to 1. So the largest prime factor can be found during this process. With this process we can efficiently find out the prime factor of any given numbers.

Algorithm

Step 1: As we have to find the largest prime factor of the given input number. So we need to define a function with the help of it we can do this task. So create a function and given it a name as primeFactor and inside this function we will pas the input number.

Step 2: After defining the function we need to define two variables called largest and current. The largest variable will store the largest prime factor and current will store the current value of the prime numbers.

Step 3: Now using a while loop to iterate the prime numbers. And this loop will run continues as long as current number will be less than or equal to the number.

Step 4: Inside the above loop we will check the main condition for finding the largest prime factor. So check that the number is divisible by the current number and giving remainder to zero. If the condition is true then update the values of largest with the current value and divide the number by the current value.

Step 5: If we have not met with the above condition then we will just increment the current value by 1.

Step 6: At the end of all the condition and by exiting the while loop we will get our largest prime factor.

Example

//Function for finding the largest prime factor
function primeFactor(number) {
  let largest = 1;
  let current = 2;

  while (current <= number) {
   if (number % current === 0) {
     largest = current;
     number /= current;
   } else {
     current++;
   }
  }

  return largest;
}
const number = 84;
const largestPrime = primeFactor(number);
console.log("The largest prime factor of", number, "is", largestPrime);

Output

The largest prime factor of 84 is 7

Complexity

The time complexity for finding the largest prime factor of a number is O(sqrt(n)), in which n is the largest prime number. The reason for this complexity is that the function is depending on the size of the given number and we have iterated through all the prime numbers up to the square root of n.

Conclusion

The function we have created allows us to find the largest prime factor for the given number in Javascript. As we have used prime factorization technique to do this task.

Updated on: 14-Aug-2023

639 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements