Factorize a number in JavaScript


In the given problem statement we are required to factorize the given number with the help of Javascript functionalities. So we will use loops and basic mathematics to factorize the given number.

Understanding the Problem

The problem at hand is to factorize the given number with the help of Javascript. So the factorization means we will have to find all the prime factors of a number. The prime factors are the prime numbers which can divide the given number without leaving a remainder. So with the help of finding the prime factors we will be able to express a number as a product of its prime factors.

Logic for the given Problem

To solve the given problem we will use a straightforward method. As we will iterate through all the numbers starting from 2 to the square root of the given number. And we will divide the given input number with each of these numbers. And we will apply a condition that the given number is divisible by a particular number, so that the number is a prime factor. So we will continue this process until it is no longer divisible. After that we will move to the next number in the iteration and repeat the process until we reach the square root of the given number. When we will be completing the loop, and if the remaining number is greater than 1 so it is also a prime factor.

Algorithm

Step 1: As we have to factorize the given number, so for solving this task we need a function named factorize and in this function we will pass a parameter named num. We will factorize the num.

Step 2: After declaring the function we will define an array to store the prime factors and Initialize this array as empty.

Step 3: Now we have an array to store the result value, now we will iterate the numbers starting from 2 to the square root of the given number.

Step 4: In this step we will check that the given number is divisible by the current number. If this condition is true then we will add the current number to the array of prime factors.

Step 5: And then we will divide the given number with the current number and do this process until it is no longer divisible.

Step 6: Now we will check the remaining number after completing the iteration is greater than 1 then we will add it to the array of prime factors.

Step 7: At the end we will return the array of prime factors as a result.

Example

// Function to factorize the given number
function factorize(number) {
   let factors = [];
   for (let i = 2; i <= Math.sqrt(number); i++) {
      while (number % i === 0) {
         factors.push(i);
         number /= i;
      }
   }
   if (number > 1) {
      factors.push(number);
   }
   return factors;
}

const number = 36;
const primeFactors = factorize(number);
console.log(`Prime factors of ${number}: ${primeFactors}`);

Output

Prime factors of 36: 2,2,3,3

Complexity

The time complexity for factorization of the given number in Javascript is O(sqrt(n)), in which n is the given number. Because we have iterated through all numbers from 2 to the square root of the given number. And the space complexity is O(1) because we have used an array to store the prime factors.

Conclusion

As a result we will develop an algorithm which gives an efficient solution to factorize the given integer number. Finding all the prime factors which divide the given number without leaving remainder is known as factorization technique.

Updated on: 14-Aug-2023

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements