Home > OS >  Iterate over an array of identical arrays and multiplying each subsequent element by x
Iterate over an array of identical arrays and multiplying each subsequent element by x

Time:09-16

I'm working through a problem that involves admirable numbers, which are defined as

A number that is equal to the sum of all its proper divisors -- provided that one of them is negative.

For example, the proper divisors of 12 are 1, 2, 3, 4, 6, totaling 16. However, if 2 is negative, the total would be 12, the number itself. 12 is therefore an admirable number.

12 = 1 - 2 3 4 6

Essentially, I have to figure out what factor should be negative to get the admirable number.

I've worked through a similar problem that deals with perfect numbers and am using a similar approach to figure solve this problem. Basically, I start out by creating an array of factors for a number that doesn't include the number itself. Then, I create an array of identical arrays that include all the factors for a number.

function admirable(n) {
  function factors(n) {
    let factors = []
      for(let i = 1; i <= n; i  ) {
        if(n % i == 0) {
            factors.push(i);
        }
      }
      return factors
    }
  
  let arr = []
  for (let i=0; i<baseArr.length; i  ) {
    arr.push(baseArr)
  }
  return arr
}
admirable(6) // [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]
admirable(12) // [
  [ 1, 2, 3, 4, 6 ],
  [ 1, 2, 3, 4, 6 ],
  [ 1, 2, 3, 4, 6 ],
  [ 1, 2, 3, 4, 6 ],
  [ 1, 2, 3, 4, 6 ]
]

Once I've done this, I want to go through each array and multiply one number by -1, such that I get the result:

admirable(6) // [ [ -1, 2, 3 ], [ 1, -2, 3 ], [ 1, 2, -3 ] ]
admirable(12) // 
// [
//  [ -1, 2, 3, 4, 6 ],
//  [ 1, -2, 3, 4, 6 ],
//  [ 1, 2, -3, 4, 6 ],
//  [ 1, 2, 3, -4, 6 ],
//  [ 1, 2, 3, 4, -6 ]
// ]

Once I get the arrays in this configuration, I figure I can complete the last part of the problem:

If n is admirable, return the proper divisor that must be rendered negative to make the sum of the proper divisors equal to n.

(I'll gladly accept any insight into ways to solve this part of the problem, too)

Is there a way to iterate over each array and make each subsequent number negative, e.g.

arr[0][0] * -1, arr[1][1] * -1 ...arr[n][n] *-1 ?

I spent a lot of time working on this yesterday and couldn't get my brain around it!

CodePudding user response:

Your Question

If you have some array like this:

let arr = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

Then you can do:

for (let i = 0; i < arr.length; i  ) {
  arr[i][i] *= -1;
}

Which gives

[[-1, 2, 3], [1, -2, 3], [1, 2, -3]]

Alternatively...

Suppose you want to check if n is admirable. Let s be the sum of the divisors of n and d be a divisor of n. If you had computed s but made d negative, the total you would have obtained is s - 2d. So n is admirable if and only if n = s - 2d for some divisor d (and then d is the number you're looking for). So you can just check if d = (s - n) / 2 is an integer and a divisor of n.

  • Related