Representing number as the power and product of primes in JavaScript

We need to write a JavaScript function that takes a positive integer and represents it as a product of prime powers. This process is called prime factorization.

For a number n, our function should return a string in the format:

n = "(p1**n1)(p2**n2)...(pk**nk)"

Where p1, p2, p3...pk are prime numbers, n1, n2...nk are their powers, and ** represents exponentiation.

Understanding Prime Factorization

Prime factorization breaks down a number into its prime factors. For example, 12 = 2² × 3¹, which would be represented as "(2**2)(3)".

Implementation

Here's a corrected and improved implementation:

const isPrime = num => {
    if (num < 2) return false;
    for (let i = 2; i <= Math.sqrt(num); i++) {
        if (num % i === 0) {
            return false;
        }
    }
    return true;
};

const primeFactors = (n) => {
    const factors = {};
    let divisor = 2;
    
    // Find all prime factors and their powers
    while (n > 1) {
        while (n % divisor === 0) {
            factors[divisor] = (factors[divisor] || 0) + 1;
            n = n / divisor;
        }
        divisor++;
        
        // Optimization: if divisor squared is greater than n, 
        // then n must be prime
        if (divisor * divisor > n && n > 1) {
            factors[n] = (factors[n] || 0) + 1;
            break;
        }
    }
    
    // Build the result string
    let result = '';
    for (const prime in factors) {
        const power = factors[prime];
        if (power > 1) {
            result += `(${prime}**${power})`;
        } else {
            result += `(${prime})`;
        }
    }
    
    return result;
};

// Test with different numbers
console.log("Prime factorization of 86240:", primeFactors(86240));
console.log("Prime factorization of 12:", primeFactors(12));
console.log("Prime factorization of 17:", primeFactors(17));
console.log("Prime factorization of 100:", primeFactors(100));
Prime factorization of 86240: (2**5)(5)(7**2)(11)
Prime factorization of 12: (2**2)(3)
Prime factorization of 17: (17)
Prime factorization of 100: (2**2)(5**2)

How It Works

The algorithm works by:

  1. Division Method: Starting with divisor 2, we repeatedly divide the number by each divisor as long as it's divisible
  2. Factor Counting: We count how many times each prime divides the number
  3. Optimization: Once divisor² > remaining number, any remaining number must be prime
  4. String Formatting: We format the output showing powers greater than 1 with ** notation

Key Points

  • The isPrime function is optimized to check divisors only up to ?num
  • We use an object to store prime factors and their counts
  • The algorithm handles edge cases like prime numbers (power = 1)
  • Time complexity is O(?n) in the worst case

Conclusion

Prime factorization is essential in number theory and cryptography. This implementation efficiently finds all prime factors and formats them as required, handling both composite and prime numbers correctly.

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

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements