How do you put each number's business in the mass?



  • It's a challenge to separate the numbers from a to b from business. These businessmen need to be recorded in a mass that will be part of another mass. This code just adds a bunch of different numbers. I need every number of businessmen to be in a separate set so they can work with them.

    function devide(x) {
        for (let a = 1; a <= x; a++) {
            if (x % a == 0) {
                divisors.push([a])
            }
        }
    }
    

    Please tell me how to fix this. Thank you.



  • function getDivisorsRange(a, b) {
        if (a > b) {
            var temp = a;
            a = b;
            b = temp;
        }
        result = new Array();
        var index = 0;
        for (let i = a; i <= b; i++) {
            result.push({
                num: i,
                divisors: new Array(),
            })
            for (let j = 1; j <= i; j++) {
                if (i % j == 0) {
                    result[index].divisors.push(j);
                }
            }
            index++;
        }
        return result;
    }
    
    getDivisorsRange(25, 40);
    

    And. The business search function is a little more optimistic, namely, to make the cycle not to the very number, but to the root and, when the divisors are found, to insert not only the number itself, but also the number that is not sufficient to obtain the number for which we are looking for the divider (unless this is the square root of this number)

    P.S. is like:

        for (let j = 1; j <= forto; j++) {
    
        if (i % j == 0) {
            result[index].divisors.push(j);
            if (j * j != i) {
                result[index].divisors.push(i / j);
            }
        }
    }
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2