Open In App

How to solve JavaScript heap out of memory on prime number ?

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript is a high-level, dynamically typed object-oriented programming language. An “out of memory” error is thrown when the program exceeds the available memory and causes overflow. It may also be caused due to an object that is too long or requires memory greater than it is allocated. Since everything in JavaScript is an object and objects are stored in heap, we often come across “heap out of memory” error while executing JavaScript programs.

Let us take a very common example of a program that determines the prime factors of a large number say 10^9 order. The way to overcome the problem of memory leakage is to use an algorithm that does not require looping through the end of the number to find the prime factors. 

Approach: The function primeFactors() accepts an argument num which is the original number to be factorized. An empty array is declared which will store the prime factors. The outer for loop loops from i=2 till i=sqrt(num). If the number is divided by the current value of i, the current value of i is pushed into the array. Since we are only concerned with finding distinct prime factors we push the factor into the array first and then keep dividing the number by i until it is no longer divisible and then it breaks the while loop with a reduced value for num. The for loop completes and the final value of num is checked. If the value of num is greater than 2 then it is the largest prime factor and is pushed into the array.

Example 1: This example shows the use of the above-described approach.

Javascript




function primeFactors(num) {
    var prime = [];
    for (i = 2; i * i <= num; ++i) {
        if (num % i === 0) {
 
            // Prime factor found
            prime.push(i);
            while (num % i === 0) {
                num /= i;
            }
        }
    }
    if (num > 2) {
 
        // Largest prime factor
        prime.push(num);
    }
    return prime;
}
 
console.log(primeFactors(992474117));


Output:

[ 23719, 41843 ]

Summary:

  1. num=992474117
  2. The prime array is declared
  3. for loop runs from i=2 to i=31503
  4. i=23719 divides num with rem=0 and 23719 is also a prime number, hence it is pushed into the array.
  5. num/i = 41843 thus num= 41843, is also a prime number that is not divisible by any number within the range of i
  6. num>2 and is the largest prime factor hence it is pushed into the array.

Example 2: This example shows the use of the above-explained approach.

Javascript




function primeFactors(num) {
    var prime = [];
    for (i = 2; i * i <= num; ++i) {
        if (num % i === 0) {
            // prime factor found
            prime.push(i);
            while (num % i === 0) {
                num /= i;
            }
        }
    }
    if (num > 2) {
 
        //largest prime factor
        prime.push(num);
    }
    return prime;
}
console.log(primeFactors(1000000000000000000));


Output:

[ 2, 5 ]

Summary:

  1. num=1000000000000000000 is an even number and is divisible by 2.
  2. 2 is pushed into the array and we keep dividing the current value of num by 2 until it is no longer divisible.
  3. Looping through the range of i, the next prime number is 5 which is pushed into the array.
  4. After the completion of the for loop num=0 and all the prime factors have been stored.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads