-2

I'm generating an array of 5 random numbers. Once generated, I want to ensure that they're all not multiples of each other (super strange edge case). If they are, I want to regenerate them. So I don't want to see [2,4,6,8,10] or [4,8,12,16,20].

How would I detect that?

LOLapalooza
  • 2,042
  • 3
  • 17
  • 22
  • You're going to need to be more precise in your requirement here. Not all multiples of the lowest (non-0, non-1?) value? Once you can state the requirement concisely and accurately, it's probably simple for you to implement. – jarmod Dec 06 '21 at 23:00
  • https://stackoverflow.com/questions/7037926/javascript-how-to-tell-if-one-number-is-a-multiple-of-another – epascarello Dec 06 '21 at 23:04
  • 3
    What does "all multiples of each other" mean? Perhaps, you could just find the smallest number in the array, and check if all other numbers are multiples of it? Or do you need something else? – Scotty Jamison Dec 06 '21 at 23:04
  • const min = Math.min(...arr); const isMultiples = arr.every(x => x % min === 0); – epascarello Dec 07 '21 at 00:05
  • Please phrase your question correctly. **They're all not multiples of each other**. How is 8 a multiple of 6? Do you mean they shouldn't have a common divisor? – Redu Dec 07 '21 at 00:05

2 Answers2

0

The most straightforward way is to sort them and then check to see if the lowest value is a divisor of all of them. If it is, check to see if the next number is a divisor of the ones that come after. etc. etc.

-2

Easiest way is to create an array of 100 prime numbers then generate 5 unique numbers under 100 and use this to get the value at the numbered index of the prime numbers array. You will then guarantee the none of the numbers are multiples of each other.

Myke Black
  • 1,299
  • 15
  • 15
  • I didn't downvote but why 100? Also, I'm guessing OP's first question would be _"how do I generate prime numbers"_ so a code example wouldn't go astray – Phil Dec 06 '21 at 23:03
  • 100 is an arbitrary number. With 6 of 100 possible options to choose from you have a very large combination of numbers.But the question is not about getting all results to be prime numbers, its about not having numbers which are multiples of each other. Having prime numbers will guarentee this, so you do not need to do an iterative check on each subsequent number to make sure that it is not a multiple of all preceding numbers, so computationally is far more efficient. – Myke Black Dec 08 '21 at 22:57