Find Prime Factors of a number in javascript?

Find Prime Factors of a number in javascript?

We will discuss two methods to solve this problem using Javascript

Play this article

Hi everyone today we Find Prime Factors of a number in javascript. let's discuss how to solve this problem statement. if you want to check out yesterday's problem click here.

Prime factorization is a way of expressing a number as a product of its prime factors. A prime number is a number that has exactly two factors, 1 and the number itself.

Example :

  • Input: 15

  • Output : 3 5

We will discuss two methods to solve this problem using Javascript

  • Method 1: Division Method.

  • Method 2: Factor Tree Method.

Method 1: Division Method

Algorithm

  • step 1: Define a function named isPrime which takes an integer n as input.

  • step 2. In the isPrime function, loop through all the integers from 2 to the square root of n.

  • step 3. If any of the integers evenly divide n, return false because n is not a prime number.

  • step 4. If none of the integers from 2 to the square root of n evenly divide n, return true because n is a prime number.

  • step 5. Define a function named primeFactors which takes an integer n as input.

  • step 6. In the primeFactors function, loop through all the integers from 2 to n.

  • step 7. For each integer i, check if it is a prime number using the isPrime function.

  • step 8. If i is a prime number, loop through n as long as it is divisible by i.

  • step 9. For each iteration of the loop in step 8, divide n by i and print i to the console.

  • step 10. After the loop in step 8 completes, move on to the next integer in the outer loop in step 6.

  • step 11. When the outer loop in step 6 completes, the function has finished executing.

  • step 12. Define a constant named n and set it equal to the value 90.

  • step 13. Call the primeFactors function and pass in n as an argument.

  • step 14. The prime factors of n will be printed on the console.

Javascript Code

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

function primeFactors(n) {
    for (let i = 2; i <= n; i++) {
        if (isPrime(i)) {
            let x = n;
            while (x % i === 0) {
                console.log(i + " ");
                x /= i;
            }
        }
    }
}

const n = 90;
primeFactors(n);

Output

2 3 3 5

Method 2: Factor Tree Method

Algorithm

  • step 1: Define a function named primeFactors which takes an integer n as input.

  • step 2: While n is even, print 2 to the console and divide n by 2.

  • step 3: Loop through odd integers from 3 up to the square root of n.

  • step 4: If i is a factor of n, print i to the console and divide n by i.

  • step 5: Repeat step 4 as long as i is a factor of n.

  • step 6: If n is greater than 2, it is also a prime factor, so print it to the console.

  • step 7: Define a constant named n and set it equal to the value 90.

  • step 8: Call the primeFactors function and pass in n as an argument.

  • step 9: The prime factors of n will be printed to the console.

Javascript Code

function primeFactors(n) {
    while (n % 2 === 0) {
        console.log(2 + " ");
        n /= 2;
    }

    for (let i = 3; i <= Math.sqrt(n); i += 2) {
        while (n % i === 0) {
            console.log(i + " ");
            n /= i;
        }
    }

    if (n > 2) {
        console.log(n);
    }
}

const n = 90;
primeFactors(n);

Output

2 3 3 5

Conclusion

So this article we solved the problem statement using javascript language. I hope you found this article helpful for your web development journey.

  • If you fill this article informative please like this article, if you have any suggestions please comment on this article, share it on social media and follow me on

  • Hashnode - Gaurav Patil

  • Twitter - @GauravYPatil