# How to generate two numbers such that the smaller divides the larger

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?

• You need two numbers $a$ and $b$ so that $a$ divides $b$ and $b$ divides $a$? – leo Jan 2 '13 at 18:02
• 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? – Shane Jordan Jan 2 '13 at 18:14
• @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. – Austin Mohr Jan 2 '13 at 18:18
• @ShaneJordan You do not need a test division (see my answer). – Austin Mohr Jan 2 '13 at 18:19
• 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. – Ross Millikan Jan 2 '13 at 18:49

Generate two numbers $a$ and $d$, let $b=ad$ and output $a$ and $b$.
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$).