Factorize a number in JavaScript

In JavaScript, factorizing a number means finding all the prime factors that multiply together to form the original number. This is a fundamental mathematical operation used in cryptography, number theory, and various algorithms.

Understanding Prime Factorization

Prime factorization breaks down a number into its basic building blocks - prime numbers. For example, 36 = 2 × 2 × 3 × 3, where 2 and 3 are prime factors. We need to find all prime numbers that divide the given number without leaving a remainder.

Algorithm Approach

The efficient approach iterates from 2 to the square root of the number. For each divisor found, we repeatedly divide the number until it's no longer divisible, then move to the next potential factor. If any remainder exists after the loop, it's also a prime factor.

Basic Implementation

function factorize(number) {
    let factors = [];
    
    // Check for factor 2
    while (number % 2 === 0) {
        factors.push(2);
        number /= 2;
    }
    
    // Check for odd factors from 3 onwards
    for (let i = 3; i <= Math.sqrt(number); i += 2) {
        while (number % i === 0) {
            factors.push(i);
            number /= i;
        }
    }
    
    // If number is still greater than 1, it's a prime factor
    if (number > 1) {
        factors.push(number);
    }
    
    return factors;
}

const number = 36;
const primeFactors = factorize(number);
console.log(`Prime factors of ${number}: [${primeFactors.join(', ')}]`);
Prime factors of 36: [2, 2, 3, 3]

Multiple Examples

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

// Test with different numbers
console.log("12 ->", factorize(12));
console.log("17 ->", factorize(17));
console.log("100 ->", factorize(100));
console.log("315 ->", factorize(315));
12 -> [ 2, 2, 3 ]
17 -> [ 17 ]
100 -> [ 2, 2, 5, 5 ]
315 -> [ 3, 3, 5, 7 ]

Enhanced Version with Unique Factors

function getUniqueFactors(number) {
    let factors = new Map();
    let original = number;
    
    // Count factor 2
    while (number % 2 === 0) {
        factors.set(2, (factors.get(2) || 0) + 1);
        number /= 2;
    }
    
    // Count odd factors
    for (let i = 3; i <= Math.sqrt(number); i += 2) {
        while (number % i === 0) {
            factors.set(i, (factors.get(i) || 0) + 1);
            number /= i;
        }
    }
    
    if (number > 1) {
        factors.set(number, 1);
    }
    
    return factors;
}

const num = 72;
const uniqueFactors = getUniqueFactors(num);
console.log(`${num} = `, Array.from(uniqueFactors.entries())
    .map(([factor, count]) => count > 1 ? `${factor}^${count}` : factor)
    .join(' × '));
72 =  2^3 × 3^2

Performance Analysis

Time Complexity: O(?n) where n is the input number, as we only iterate up to the square root.

Space Complexity: O(log n) for storing the prime factors in the worst case.

Key Optimization

The algorithm checks even numbers (factor 2) separately, then only checks odd numbers starting from 3. This reduces iterations by half compared to checking every number from 2 to ?n.

Conclusion

Prime factorization in JavaScript efficiently decomposes numbers into their prime components using iteration up to the square root. This approach provides optimal performance for most practical applications while maintaining code simplicity.

Updated on: 2026-03-15T23:19:00+05:30

897 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements