Find two numbers which are divisible without remainder [closed]
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
.
精彩评论