Open In App

JavaScript Program to Find Prime Factors

Last Updated : 04 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see all the methods to find the prime factors of a number using JavaScript.

Methods to Find Prime Factors:

Method 1: Using loops and Math.pow() Method

In this method, we will use a JavaScript loop to iterate the possible factors and Math.pow() method to get the square root of the number. Instead of Math.pow() method, we can also use Math.sqrt() or i*i < n condition.

Example:

JavaScript
function isPrime(i) {
    if (i == 2) return true;
    for (let j = 2; j < Math.pow(i, 0.5) + 1; ++j) {
        if (i % j === 0) return false;
    }
    return true;
}
function prime(n) {
    let result = [];
    for (let i = 2; i < Math.pow(n, 0.5); i++) {
        if (n % i == 0 && isPrime(i)) result.push(i);
        if (n % i == 0 && isPrime(n/i)) result.push(n/i);

    }
    return result.sort((a,b)=>a-b);
}

const num = 85;
console.log("Prime factors of " + 
    num + ": " + prime(num));

Output
Prime factors of 85: 5,17

Method 2: Recursive Approach with Spread Operator

In this method, we will call the function recursively and return the output with the spread operator to get the array output.

Example:

JavaScript
function recursivePrime(n, d) {
    if (n <= 1) return [];
    if (n == 2) return [2];
    flag = false;
    while (n % d == 0) {
        n /= d;
        flag = true;
    }
    if (flag) return [d, ...recursivePrime(n, d + 1)];
    return recursivePrime(n, d + 1);
}

const num = 85;
console.log(
    "Prime factors of " +
        num + ": " +
        recursivePrime(num, 2)
);

Output
Prime factors of 85: 5,17

Method 3: Sieve of Eratosthenes Algorithm

The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a specified integer. We can modify this algorithm slightly to find the prime factors of a given number.

Example:

JavaScript
function primeFactorsSieve(n) {
    const primes = [];
    const smallestPrimeFactor = new Array(n + 1).fill(0);
    
    for (let i = 2; i <= n; ++i) {
        if (smallestPrimeFactor[i] === 0) {
            smallestPrimeFactor[i] = i;
            primes.push(i);
        }
        
        for (let j = 0; j < primes.length && primes[j] <= smallestPrimeFactor[i] && i * primes[j] <= n; ++j) {
            smallestPrimeFactor[i * primes[j]] = primes[j];
        }
    }
    
    const factors = [];
    while (n > 1) {
        factors.push(smallestPrimeFactor[n]);
        n /= smallestPrimeFactor[n];
    }
    
    return factors;
}

const num = 85;
console.log("Prime factors of " + num + ": " + primeFactorsSieve(num));

Output
Prime factors of 85: 5,17

Prime Factors Using JavaScript
Visit Course explore course icon

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy