开发者

Find two numbers which are divisible without remainder [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

The numbers are generated randomly. In the specified range. For example, it is two numbers:开发者_开发技巧 5 and 10, well 10 divides 5. If it's 5 and 2, it is not divide. The 2 required to reduce by 1 or 5 to increase by 1. Tell me a fast algorithm?


1. Let A is greater and B is smaller
2. Set M = (A % B)
3. If M == 0, You're done..
4. Else Adjust A  either by adding, A = A + B - M
5.               or by subtracting, A = A - M


a%b==0 is true if b divides a without remainder


if(!(temp1 % temp2))) 
    temp1= temp1 + (temp1%temp2)
else 
   tadaaaaa :)


if (val1 > val2) { 
   rem = val1 % val2 ;
   if (rem == 0) you're done
   otherwise required_addition = val2 + rem
} else if (val1 < val2) { 
    required_addition = val2 - val1;
} else {
    they are the same; you are done;
}


Rather than using division, it would be easier to construct a pair of numbers that can be divided with no remainder. Pick the first number at random, a say, then pick another number at random, b say, and then set c = a*b.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜