Question #38744

Question: Find all the combinations between 1 and 1000 in which


1. doubling the first number and substracting from the second number will give a result of zero.


2. if the result is not 0, double the result and use it as the first number and repeat step 1.




For example :

let us consider 7 , 8 as inputs

step 1 :

7 * 2 - 8 = 6, which is not zero, so it goes to next step and first number is 6

6 * 2 - 8 = 4, which is not zero, so it goes to next step and first number is 4

4 * 2 - 8 = 0, here it satisfies the condition so system will print 7 , 8



Another sample set that satisfies the above condition: 10, 20

10 * 2 - 20 = 0, here it satisfies the condition so system will print 10 , 20
1

Expert's answer

2014-01-30T09:37:02-0500
#include <iostream>
using namespace std;
bool checkPair(int a, int b)
{
    int res;
    while(1)
    {
        if (a >= b)
            return false;
        res = a*2 - b;
        if (res == 0)
            return true;
        else if (res < 0)
            return false;
        else
            a = res;
    }
}
int main()
{
    for (int i = 1; i <= 1000; i++)
    {
        for (int j = 1; j <= 1000; j++)
            if (checkPair(i, j))
                cout << i << ' ' << j << std::endl;
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS