Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How To Find Multiplication Factors of a negative number

I wanna find the factors of a negative number in multiplication

Somethings not right on my method, it only works on positive numbers not negative

I have this and it did not work with negative numbers

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function factorMul(num) {

  let tryThis1 = Math.floor(

    Math.random() * (num - 1 + 1) + 1

  )

  let tryThis2 = Math.floor(

    Math.random() * (num - 1 + 1) + 1

  )

  if(Math.floor(tryThis1 * tryThis2) !== num) return factorMul(num)

  return [tryThis1, tryThis2]

}

When i tried it with negative numbers i got maximum call stack size

>Solution :

Find Multiplication Factors of a negative number:

  • Essentially, to factor a negative number, find all of its positive
    factors, then duplicate them and write a negative sign in front of
    the duplicates. For instance, the positive factors of −3 are 1 and 3.
    Duplicating them produces 1, 3, 1, 3; writing a negative sign before
    the duplicates produces 1, 3, −1, −3, which are all of the factors of
    −3.

So, in your code, just check if the number passed is negative, if it was, make it positive, then when it is ready to return the values, do the hint above.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading