U
U
Umid2016-10-15 11:13:36
Algorithms
Umid, 2016-10-15 11:13:36

How to optimize the algorithm for finding a number with 500 divisors?

There is a sequence of triangular numbers.
Nadi find a triangular number that has more than 500 divisors.
There is an algorithm:

var result = 0;
var resultArray = []; // Массив треугольных чисел
var measureList = []; // Массив делителей

for (let i = 1; i <= 1000; i++) {
  result =  i + result;
  resultArray.push(result);

  function getMeasure(result) {
    console.log(result); //Выводим наибольшее треугольное число которое получилось.
    for (let k = 1; k <= result; k++) {
      let measure = result / k;
      if(measure % 1) {

      } else {
        measureList.push(measure);
      }
    }
  }

  if(i == 1000) {
    getMeasure(result);
  }
}

console.log(measureList);

This code works instantly.
But to find a number with 500 divisors , you have to wait a very long time (Even a very long time).
To do this, at the beginning of the for loop: replace i <= 1000 with i <= 1000000 (I took the value by eye).
I can not think of an optimal algorithm in any way.
Maybe something to read?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-10-15
@DarCKoder

To factorize a number, not all options in a row are checked, but only prime numbers. The table can be prepared in advance or generated in the program. To decompose the number N 0 , it is necessary to check prime numbers up to N 0 1/2 inclusive. After each factor found, the current value of N i is divided by it, and the enumeration of prime numbers starts over.
It turns out a decomposition of the form
N 0 = p 1 n 1 ·p 2 n 2 ·...·p k n k , where p i are all prime numbers from 2 to N 0 1/2 .
The number of different divisors K is obtained as the number of combinations of powers
K = (n 1 +1) (n 2 +1) ... (n k +1)
For example:
N 0 = 72
Prime numbers up to 72 1/2 : p i = [2, 3, 5, 7]
Initialize the counters of degrees: n i = [ 0 , 0, 0, 0] 1 = 2, check 36/2 = 18 18 is divisible by 2, n 1 = 3, check 18/2 = 9 9 is not divisible by 2 9 is divisible by 3, n 2 = 1, check 9/3 = 3
3 by 2 is not divisible
3 by 3 is divisible, n 2 = 2, 2/3 = 1, finished searching.
We got an array of degrees: n i = [3, 2, 0, 0]
Number of divisors: K = (3+1) (2+1) (0+1) (0+1) = 12

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question