Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question
1  
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
2  
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

2 Answers 2

up vote 8 down vote accepted

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

share|improve this answer
    
I generated b and the answer, then multiplied them to give me the first answer. That worked great, thank you. –  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$).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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