I am creating a children's math game and need an algorithm (that I can write in JavaScript) to generate two numbers such that the smaller always divides the larger. How can I do that?

  • 1
    $\begingroup$ You need two numbers $a$ and $b$ so that $a$ divides $b$ and $b$ divides $a$? $\endgroup$ – leo Jan 2 '13 at 18:02
  • $\begingroup$ Yes, so i randomly generate a and b. a = 4 and b = 2 for example, great, answer is 2 with no remainder, but how do I not generate things like a = 9 and b = 6? Do I just need to try one test it and then generate another set if there is a remainder? $\endgroup$ – Shane Jordan Jan 2 '13 at 18:14
  • $\begingroup$ @ShaneJordan The confusion is that you asked for two numbers that will always divide evenly into each other, but you seem to mean that one should divide evenly into the other. $\endgroup$ – Austin Mohr Jan 2 '13 at 18:18
  • $\begingroup$ @ShaneJordan You do not need a test division (see my answer). $\endgroup$ – Austin Mohr Jan 2 '13 at 18:19
  • 2
    $\begingroup$ The approach in the answers is quite general. Generate the solution, and form the problem from it. So you generate the divisor and the quotient. Either one will divide evenly into their product. $\endgroup$ – Ross Millikan Jan 2 '13 at 18:49

Generate two numbers $a$ and $d$, let $b=ad$ and output $a$ and $b$.

  • $\begingroup$ I generated b and the answer, then multiplied them to give me the first answer. That worked great, thank you. $\endgroup$ – Shane Jordan Jan 2 '13 at 21:55

Generate two random integers $a$ and $b$. Set the dividend as $ab$ and the divisor as $b$. In this way, you are sure that the divisor evenly divides the dividend and gives the quotient $a$ (i.e. $ab \div b = a$).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.